Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do you need to bind a function in a constructor

Tags:

reactjs

redux

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

like image 355
Saad Avatar asked Jul 12 '16 16:07

Saad


People also ask

Why do we bind a constructor?

When we bind the this of the event handler to the component instance in the constructor, we can pass it as a callback without worrying about it losing its context. Arrow functions are exempt from this behavior because they use lexical this binding which automatically binds them to the scope they are defined in.

Why do you need to bind function?

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.

Why bind is required in react?

The bind() is an inbuilt method in React that is used to pass the data as an argument to the function of a class based component. Syntax: this. function.

Why do we need to bind this in our callbacks in class based components *?

This is because whenever inside a class component when we need to pass a function as props to the child component, we have to do one of the following: Bind it inside the constructor function. Bind it inline (which can have some performance issues).


3 Answers

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.

like image 168
Tom Fenech Avatar answered Oct 23 '22 16:10

Tom Fenech


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.

like image 7
julianljk Avatar answered Oct 23 '22 15:10

julianljk


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

like image 3
Piyush.kapoor Avatar answered Oct 23 '22 16:10

Piyush.kapoor