39

I have a question relavent to this code: https://github.com/reactjs/redux/blob/master/examples/async/containers/App.js

specifically:

  constructor(props) {
    super(props)
    this.handleChange = this.handleChange.bind(this)
    this.handleRefreshClick = this.handleRefreshClick.bind(this)
  }

I guess its a 2 part question.

  1. Why do I need to set handle change as an instance of class this.handleChange =, can't I just use static functions for handleChange and call it directly with in the class onClick={handleRefreshClick}> ?
  2. I have no idea whats going on here: this.handleRefreshClick.bind(this)

Thanks

Saad
  • 19,836
  • 13
  • 41
  • 68
  • 1
    Possible duplicate of [Use of the JavaScript 'bind' method](http://stackoverflow.com/questions/2236747/use-of-the-javascript-bind-method) – ctrlplusb Jul 12 '16 at 16:14
  • not exactly, I dont quite understand in context of a class, especially number 2 – Saad Jul 12 '16 at 16:15
  • It doesn't change the meaning of `bind`. You use `bind` to maintain the scope to the `this`. In the context of react this allows you to call things like `this.setState` etc. – ctrlplusb Jul 12 '16 at 16:16

5 Answers5

53

Answered in reverse order...

  1. this.handleRefreshClick.bind(something) returns a new function, in which references to this will refer to something. This is a way of saving the current value of this, which is in scope during the call to the constructor, so that it can be used later when the function is called.
  1. If your functions don't require access to the state of your component, then sure, you don't need to bind them.

The argument in favour of adding these lines to the constructor is so that the new bound functions are only created once per instance of the class. You could also use

onClick={this.handleRefreshClick.bind(this)}

or (ES6):

onClick={() => this.handleRefreshClick()}

but either of these methods will create a new function every time the component is re-rendered.

Tom Fenech
  • 65,210
  • 10
  • 85
  • 122
  • 2
    but doing .bind(this) the whole idea of a class is that it encapsulates 'this' right, so why do I need to encapsulate the scope in a specific function when the entire instance of a class should encapsulate the scope – Saad Jul 12 '16 at 16:22
  • 5
    @Saad not in JS! Classes are really [just fancy functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes); they don't do anything particularly useful with `this`. – Tom Fenech Jul 12 '16 at 16:31
  • It is not good to bind or use arrow function in Render as it leads to reallocation of the function at every render. The better approach is to bind in constructor or use arrow functions in class. https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56 – Black Oct 04 '17 at 15:32
  • 1
    Yep, that's what it says in my answer :) – Tom Fenech Oct 04 '17 at 15:36
  • I believe this answer could be improved upon by clarifying that this is only relevant to `ReactJS` and in normal circumstances there's no need to bind functions to `this` (in regular JS classes) – pzaj Mar 19 '18 at 09:18
  • 1
    @user1970395 on the contrary, I wouldn' t say that there's much in this answer which _is_ React-specific. Any class methods used in event handlers will always need `this` to be bound, if they access properties on the instance. – Tom Fenech Mar 19 '18 at 09:30
  • @TomFenech Hello, im new to react and javascript in general, i read that if you add a function to a class it gets attached to it's prototype. So won't it be able to reference the function `this.handleClick` by looking up through the prototype chain in case of React classes ? I didn't understand the last point you explained to @user1970395, Are you saying since the event handler is a function itself, `this` in that function will refer to the event handler's `this`? – abhinavm93 Apr 02 '18 at 11:54
  • 3
    @abhinavm93 I'm saying that unless you explicitly bind `this` to a function, its value depends on the context in which it is called. React uses the render function to create DOM elements with event handlers. From the context in which these events are handled, there is no knowledge of the class that these event handlers belong to, unless you bind it. – Tom Fenech Apr 02 '18 at 13:03
4

The reason why it's being done, is to bind the this keyword to that object. Like Tom said, calling a function from a class doesn't mean it's being called with the context of the object that created that function.

I think you might be getting confused because in the React examples/tutorials, using React.createClass() DOES bind this automatically for you. So you might be wondering why React.createClass() does it, but doesn't with ES6 class syntax.

This is because React didn't want to mess with ES6 specifications (binding this to functions from its class is not in the ES6 class spec), but at the same time, wanted to give its users the convenience of ES6 class syntax. You can read more about this below.

Github issue

Hopefully that sheds some light on why that happens.

julianljk
  • 1,038
  • 9
  • 11
3

this depends how the function is called, not how/where it is created.

When you look at the code, you see two "this", why? Seems weird, right? The thing is it is not about how it seems. It is about how it is called.

You are basically saying. Hey, when somebody calls you remember this means this class. not something else.

When somebody calls your class like: x.yourClass().bind(this) you are saying this is not x but the class itself(with props and states etc.).

Quick note, even when you call directly the class like yourClass() you are actually calling window.yourClass() on browser, also that is why in this case the this is window.

serkan
  • 5,565
  • 2
  • 33
  • 43
2

These 2 functions handleChange and handleRefreshClick are passed down as props to other components ,

They are bind to this because when the child component will call these functions they will always execute with the APP context.

You can remove these functions from the class but still you need to bind this since you would be updating some parts of your APP

Piyush.kapoor
  • 6,041
  • 1
  • 17
  • 18
0

I personally bind functions in constructor so that their references don't change on each re-render.

This is especially important if you are passing functions to read-only children that you don't need to get updated when their props don't change. I use react-addons-pure-render-mixin for that.

Otherwise, on each parent's re-render, binding will happen, new function reference will get created and passed to children, which is going to think that props have changed.

Anton Kuzmin
  • 801
  • 1
  • 10
  • 25