Add event handler to React.DOM element dynamically

前端 未结 4 546
无人及你
无人及你 2021-02-01 06:54

I\'m working with a RadioButtonGroup component which is like radio input but with buttons:

\"enter

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 07:41

    This is my code .I do not know whether this is the best way ,as i started using react just 5 hrs ago. But it works fine.

    var LeftPane = React.createClass({
        getInitialState: function() {
            return {list:[{name:"Item 1",isactive:1},
                          {name:"Item 2",isactive:0},
                          {name:"Item 3",isactive:0},                     
                          {name:"Item 4",isactive:0},
                          {name:"Item 5",isactive:0}]};
        },
        clickHandler: function(index) {
            var current_list = this.state.list;
            current_list.map(function(record,i){
              if(record.isactive==1){
                if(i!=index){
                  record.isactive =0;
                }else{
                  return;
                }
              }else{
                if(i==index){
                  record.isactive =1;
                }
              }
            });
            this.setState({data:current_list}); 
        },   
        render: function() {
            var active_item = "list-group-item active"
            var non_active_item ="list-group-item";
            var _this = this;
            return (              
              
    {this.state.list.map(function(record,i) { if(record.isactive==1){ return {record.name} }else{ return {record.name} } })}
    ) } });

提交回复
热议问题