Why does React warn me against binding a component method to the object?

后端 未结 2 1758
暖寄归人
暖寄归人 2020-12-18 21:52

I have this here component. I want to pass down a call handler to each listElement I create. If I do it like below, with bind(this), it works properly. The prob

相关标签:
2条回答
  • 2020-12-18 22:36

    The error is coming from somewhere else in the code. You get the error when you do this.someFunction.bind(something) and something isn't null.

    this.someFunction.bind({}, foo);    // warning
    this.someFunction.bind(this, foo);  // warning, you're doing this
    this.someFunction.bind(null, foo);  // okay!
    

    Do a search for .bind(this in your code to find the offending line.

    0 讨论(0)
  • 2020-12-18 22:36

    Here is updated docs example

    React.createClass({
      onClick: function(event) {/* do something with this */},
      render: function() {
        return <button onClick={this.onClick} />;
      }
    });
    

    Update in ReactJS Docs

    0 讨论(0)
提交回复
热议问题