13

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 problem is that I get this warning from React in the console: bind(): You are binding a component method to the component. React does this for you automatically in a high-performance way, so you can safely remove this call.

var MyList = React.createClass({
  render: function () {
    var listElements = this.props.myListValues.map(function (val) {
      return (
        <ListElement onCallHandler={this.props.parentsCallHandler} val={val} />
        );
    }.bind(this));
    return (
      <ul>
          {listElements}
      </ul>
      );
  }
});

If I don't bind it, my children don't know about the call handler. What could I have done differently?

PS:

I know about destructuring assignments, like explained http://facebook.github.io/react/docs/transferring-props.html#transferring-with-...-in-jsx, but I don't want to use Harmony.

2
  • By "call handler" do you mean event handler? Events should bubble up to the parent and can be captured there. What am I missing? Commented Nov 6, 2014 at 18:35
  • 1
    I'm not seeing that error: jsfiddle.net/s6dok0xv. Commented Nov 6, 2014 at 18:41

2 Answers 2

31

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh, you're right, it was somewhere else. I'm embarrassed now
@FakeRainBrigand It seems that it now doesn't warn in the second case, .bind(this, foo). It does with .bind(this). Tested with [email protected]. jsfiddle.net/69z2wepo/17453 And the docs currently show doing just that: facebook.github.io/react/tips/…. This story of when to bind and when not to in React seems perhaps overly complicated.
The reason why .bind(null, works is that, since react autobinds since v0.4, by doing .bind(null, we are essentiall execute this.methond.bind(this).bind(null, arg1, arg2). Just thought some of you may want to know why it worked.
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.