Browse Source

make sure param-passing example uses keys realistically

main
Alex Krolick 7 years ago
parent
commit
d7beebb65a
  1. 30
      content/docs/faq-functions.md

30
content/docs/faq-functions.md

@ -113,22 +113,23 @@ This is equivalent to calling `.bind`:
#### Example: Passing params using arrow functions #### Example: Passing params using arrow functions
```jsx ```jsx
class Component extends React.Component { const A = 65 // ASCII character code
class Alphabet extends React.Component {
state = { state = {
justClicked: 0, justClicked: null,
items: Array.from({length: 5}, (_, i) => i) letters: Array.from({length: 26}, (_, i) => String.fromCharCode(A + i))
} }
handleClick = i => this.setState({ justClicked: i }) handleClick = letter => this.setState({ justClicked: letter })
render () { render () {
return ( return (
<div> <div>
Just clicked: {this.state.justClicked} Just clicked: {this.state.justClicked}
<ul> <ul>
{ this.state.items.map(i => { this.state.letters.map(letter =>
<li onClick={() => this.handleClick(i)}> <li key={letter} onClick={() => this.handleClick(letter)}>
Item: {i} {letter}
</li> </li>
) } ) }
</ul> </ul>
@ -143,15 +144,16 @@ class Component extends React.Component {
Alternately, you can use DOM APIs to store data needed for event handlers. Consider this approach if you need to optimize a large number of elements or have a render tree that relies on React.PureComponent equality checks. Alternately, you can use DOM APIs to store data needed for event handlers. Consider this approach if you need to optimize a large number of elements or have a render tree that relies on React.PureComponent equality checks.
```jsx ```jsx
class Component extends React.Component { const A = 65 // ASCII character code
class Alphabet extends React.Component {
state = { state = {
justClicked: 0, justClicked: null,
items: Array.from({length: 5}, (_, i) => i) letters: Array.from({length: 26}, (_, i) => String.fromCharCode(A + i))
} }
handleClick = event => { handleClick = event => {
this.setState({ this.setState({
justClicked: event.target.dataset.i justClicked: event.target.dataset.letter
}) })
} }
@ -160,9 +162,9 @@ class Component extends React.Component {
<div> <div>
Just clicked: {this.state.justClicked} Just clicked: {this.state.justClicked}
<ul> <ul>
{ this.state.items.map(i => { this.state.letters.map(letter =>
<li data-i={i} onClick={this.handleClick}> <li key={letter} data-letter={letter} onClick={this.handleClick}>
Item: {i} {letter}
</li> </li>
) } ) }
</ul> </ul>

Loading…
Cancel
Save