Skip to content

Commit d7beebb

Browse files
committed
make sure param-passing example uses keys realistically
1 parent 3a029d5 commit d7beebb

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

content/docs/faq-functions.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,23 @@ This is equivalent to calling `.bind`:
113113
#### Example: Passing params using arrow functions
114114

115115
```jsx
116-
class Component extends React.Component {
116+
const A = 65 // ASCII character code
117+
class Alphabet extends React.Component {
117118
state = {
118-
justClicked: 0,
119-
items: Array.from({length: 5}, (_, i) => i)
119+
justClicked: null,
120+
letters: Array.from({length: 26}, (_, i) => String.fromCharCode(A + i))
120121
}
121122

122-
handleClick = i => this.setState({ justClicked: i })
123+
handleClick = letter => this.setState({ justClicked: letter })
123124

124125
render () {
125126
return (
126127
<div>
127128
Just clicked: {this.state.justClicked}
128129
<ul>
129-
{ this.state.items.map(i =>
130-
<li onClick={() => this.handleClick(i)}>
131-
Item: {i}
130+
{ this.state.letters.map(letter =>
131+
<li key={letter} onClick={() => this.handleClick(letter)}>
132+
{letter}
132133
</li>
133134
) }
134135
</ul>
@@ -143,15 +144,16 @@ class Component extends React.Component {
143144
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.
144145

145146
```jsx
146-
class Component extends React.Component {
147+
const A = 65 // ASCII character code
148+
class Alphabet extends React.Component {
147149
state = {
148-
justClicked: 0,
149-
items: Array.from({length: 5}, (_, i) => i)
150+
justClicked: null,
151+
letters: Array.from({length: 26}, (_, i) => String.fromCharCode(A + i))
150152
}
151153

152154
handleClick = event => {
153155
this.setState({
154-
justClicked: event.target.dataset.i
156+
justClicked: event.target.dataset.letter
155157
})
156158
}
157159

@@ -160,9 +162,9 @@ class Component extends React.Component {
160162
<div>
161163
Just clicked: {this.state.justClicked}
162164
<ul>
163-
{ this.state.items.map(i =>
164-
<li data-i={i} onClick={this.handleClick}>
165-
Item: {i}
165+
{ this.state.letters.map(letter =>
166+
<li key={letter} data-letter={letter} onClick={this.handleClick}>
167+
{letter}
166168
</li>
167169
) }
168170
</ul>

0 commit comments

Comments
 (0)