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 within
render). 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 component (which I mostly use it)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…