Ravenous Part 3 Bind

Hey, I just wanted to know why are we using this bind method inside the renderSortByOptions() method?
Why didn’t we just bind this method inside the constructor with the simple this.renderSortByOptions.bind(this) ???

  renderSortByOptions(){
    return Object.keys(sortByOptions).map(sortByOption => {
      let sortByOptionValue = sortByOptions[sortByOption];
      return <li key={sortByOptionValue} className={this.getSortByClass(sortByOptionValue)} onClick={this.handleSortByChange.bind(this, sortByOptionValue)}>{sortByOption} </li>
    });
  }
3 Likes

I have the same question. I do not understand why we bind this regarding the SortByOptions in the render-method and later on bind this regarding location and term in the constructor-method.

Hi, you can bind in constructor which is ES2015 or you can bind in render. When you use function.prototype.bind in render, it creates a new function each time the component renders which could affect the performance of your app.