假设我有一个函数generateList()
来更新状态并将它映射到一个 onClick 到一个<li>.
<li className="some-classname"}
onClick={this.generateList('product')}> Product </li>
有时我会遇到以下错误:
Warning: setState(...): Cannot update during an existing state transition (such as within
使成为). Render methods should be a pure function of props...
还有这样的。我在互联网上寻找答案,并得到了这样的答案:
<li className="some-classname"}
onClick={this.generateList.bind(this, 'product')}> Product </li>
但是我也看到了一个答案(在 Github 中,但似乎找不到)
<li className="some-classname"}
onClick={() => this.generateList('product')}> Product </li>
主要区别是什么?哪个更合适、更有效?什么是我们应该用这样的理由.bind
和() =>
映射功能的时候onClick
或者的属性作出react成分(我主要是使用它)?