Petr F.
Petr F.

Reputation: 161

What does bind(this) in constructor do in ReactJS

I am familiar with Javascript function bind. But I don't understand why in following React.js snippet is this bind again to this. Has is something common with constructor, because this in constructor can have different values depending on usage?

Thank you in advance for replies

class QuotesLibrary extends React.Component {
  constructor(props) {
    super(props);
    this.search = debounce(this.search.bind(this), 300);
  }
  search(searchTerm) {
    this.props.relay.setVariables({searchTerm});
  }
  render() {
    return (
      <div className="quotes-library">
        <SearchForm searchAction={this.search} />
        <div className="quotes-list">
          {this.props.library.quotesConnection.edges.map(edge =>
            <Quote key={edge.node.id} quote={edge.node} />
          )}
        </div>
      </div>
    )
  }
}

Upvotes: 4

Views: 8587

Answers (4)

casraf
casraf

Reputation: 21684

Here's an example of how the difference works -

As you can see, the first call will log 'undefined' and the second one will log 'Bar', because the 1st call wasn't binded, and calling functions indirectly (as promise results or as callbacks) doesn't keep the reference to this when it runs - bind tells it what its this is referring to.

function debounce(fn, to) {
    setTimeout(fn)
}

class Foo {
  constructor () {
    this.fullName = 'Bar'
  }
  
  speak () {
    console.log("My name is", this.fullName)
  }
  
  test () {
    debounce(this.speak, 1000) // undefined
    debounce(this.speak.bind(this), 2000) // "Bar"
    
  }
}

let foo = new Foo()

foo.test()

Upvotes: 4

Shubham Khatri
Shubham Khatri

Reputation: 281636

What this.search.bind(this) does it that it is binding the key this inside to the function to the context of your React Component and what it basically means is that whenever you try to access a property of the React Component, you can access it like this.props since this will then refer to the React Component's context and not the function itself.

The significance of this.search before bind is that it is trying to access the function search which is in the context of the React Component and hence you are only binding it once and not twice.

I hope I was able to explain the situation properly

Upvotes: 6

Alexandre Nicolas
Alexandre Nicolas

Reputation: 1949

You shouldn't use Function.bind(this) : you should use arrow function. Arrow functions are bind to the class (so to the component).

class QuotesLibrary extends React.Component {
  constructor(props) {
    super(props);
    this.search = debounce(this.search, 300);
  }

  search = (searchTerm) => {
    this.props.relay.setVariables({searchTerm});
  }

  render() {
    return (
      <div className="quotes-library">
        <SearchForm searchAction={this.search} />
        <div className="quotes-list">
          {this.props.library.quotesConnection.edges.map(edge =>
            <Quote key={edge.node.id} quote={edge.node} />
          )}
        </div>
      </div>
    )
  }
}

Upvotes: 6

Martin Dawson
Martin Dawson

Reputation: 7656

Why are you saying "again"? You only bind it once, not twice.

debounce from _underscores library takes a function and returns another, therefore to get the this context in the search function you need to bind it to the search.

It's the exact same as binding functions normally in the constructor.

Upvotes: 1

Related Questions