最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference of .bind() to arrow function () => usage in React - Stack Overflow

programmeradmin3浏览0评论

Suppose that I have a function generateList() that updates the state and mapping it to an onClick to a <li>.

<li className="some-classname"}  
    onClick={this.generateList('product')}> Product </li>

There are times that I encounter errors like:

Warning: setState(...): Cannot update during an existing state transition (such as withinrender). Render methods should be a pure function of props...

And such. I mined the internet for answers for this, and came upon such answer like:

<li className="some-classname"}  
    onClick={this.generateList.bind(this, 'product')}> Product </li>

But I saw one answer too (in Github, but can't seem to find it) that

<li className="some-classname"}  
    onClick={() => this.generateList('product')}> Product </li>

What's the major difference? Which is more appropriate and efficient? And what's the reason that we should use such .bind and () => when mapping a function to an onClick or as a property of a React ponent (which I mostly use it)?

Suppose that I have a function generateList() that updates the state and mapping it to an onClick to a <li>.

<li className="some-classname"}  
    onClick={this.generateList('product')}> Product </li>

There are times that I encounter errors like:

Warning: setState(...): Cannot update during an existing state transition (such as withinrender). Render methods should be a pure function of props...

And such. I mined the internet for answers for this, and came upon such answer like:

<li className="some-classname"}  
    onClick={this.generateList.bind(this, 'product')}> Product </li>

But I saw one answer too (in Github, but can't seem to find it) that

<li className="some-classname"}  
    onClick={() => this.generateList('product')}> Product </li>

What's the major difference? Which is more appropriate and efficient? And what's the reason that we should use such .bind and () => when mapping a function to an onClick or as a property of a React ponent (which I mostly use it)?

Share Improve this question edited May 23, 2017 at 11:45 CommunityBot 11 silver badge asked Sep 27, 2016 at 2:38 anobilisgorseanobilisgorse 9063 gold badges12 silver badges25 bronze badges 1
  • Does this answer your question? Binding vs arrow-function (JavaScript or react onClick) – Top-Master Commented Jul 23, 2021 at 17:09
Add a ment  | 

1 Answer 1

Reset to default 16

If you try:

<li className="some-classname"}  
    onClick={this.generateList('product')}> Product </li>

That function will be executed on every render - which can produce an additional render, which is what throws the error. What we want is to define a function that will be called later when onClick is fired.

The second one is defining a method and .bind is binding the context of the React class this to the method. Note that the bind function returns a copy of a function - So this doesn't call the function, just defines it for the onClick handler to use.

The last one:

<li className="some-classname"}  
    onClick={() => this.generateList('product')}> Product </li>

This defines an anonymous function but, similar to the second implementation, does not call it. Only when onClick is fired is it called. However in some cases using an anonymous function can cause performance issues. That anonymous function will be defined on every render - and if you have a ponent that is re-rendering very often it can hurt the performance of your application. If you are sure that the ponent will not be rendered often, an anonymous function should be fine for convenience.

Additionally when using bind you can declare it in the ponent class constructor like this:

  constructor(props) {
    super(props);
    this.state = {
      // your state
    };
    this.generateList = this.generateList.bind(this);
  }
发布评论

评论列表(0)

  1. 暂无评论