React JS onClick event handler

前端 未结 11 1131
孤街浪徒
孤街浪徒 2020-11-28 21:35

I have

var TestApp = React.createClass({
      getComponent: function(){
          console.log(this.props);
      },
      render: function(){
        retur         


        
11条回答
  •  一生所求
    2020-11-28 21:47

    You can make use of the React.createClone method. Create your element, than create a clone of it. During the clone's creation, you can inject props. Inject an onClick : method prop like this

    { onClick : () => this.changeColor(originalElement, index) }

    the changeColor method will set the state with the duplicate, allowing you sto set the color in the process.

    render()
      {
        return(
          
      {this.state.items.map((val, ind) => { let item =
    • {val}
    • ; let props = { onClick: () => this.Click(item, ind), key : ind, ind } let clone = React.cloneElement(item, props, [val]); return clone; })}
    ) }

提交回复
热议问题