ReactJS modify parent state from child component

前端 未结 1 528
暗喜
暗喜 2021-02-03 10:14

I\'m trying to remove an item from my state array when clicked. At the moment I have an onclick listener which calls a function passed into the props. However I get a warning: b

相关标签:
1条回答
  • 2021-02-03 11:09

    React actually auto-binds methods to the current component:

    http://facebook.github.io/react/blog/2013/07/02/react-v0-4-autobind-by-default.html

    In the TodoList component, rather than:

    scope.props.onRemoveItem.bind(this, i)
    

    Try:

    scope.props.onRemoveItem.bind(null, i)
    

    By providing null instead of this you'll allow React to do its own thing. Also you need to actually use the onClick handler:

    <li onClick={this.props.onClick}>{this.props.task}</li>
    
    0 讨论(0)
提交回复
热议问题