mirror of
https://github.com/airbnb/javascript.git
synced 2026-04-25 03:00:19 -04:00
[guide] [react] add more context about arrow functions, as props and in class fields
This commit is contained in:
committed by
Jordan Harband
parent
495a62aaa9
commit
be07f7a020
@@ -531,7 +531,7 @@ We don’t recommend using indexes for keys if the order of items may change.
|
||||
|
||||
## Methods
|
||||
|
||||
- Use arrow functions to close over local variables.
|
||||
- Use arrow functions to close over local variables. It is handy when you need to pass additional data to an event handler. Although, make sure they [do not massively hurt performance](https://www.bignerdranch.com/blog/choosing-the-best-approach-for-react-event-handlers/), in particular when passed to custom components that might be PureComponents, because they will trigger a possibly needless rerender every time.
|
||||
|
||||
```jsx
|
||||
function ItemList(props) {
|
||||
@@ -540,7 +540,7 @@ We don’t recommend using indexes for keys if the order of items may change.
|
||||
{props.items.map((item, index) => (
|
||||
<Item
|
||||
key={item.key}
|
||||
onClick={() => doSomethingWith(item.name, index)}
|
||||
onClick={(event) => doSomethingWith(event, item.name, index)}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
@@ -550,7 +550,7 @@ We don’t recommend using indexes for keys if the order of items may change.
|
||||
|
||||
- Bind event handlers for the render method in the constructor. eslint: [`react/jsx-no-bind`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md)
|
||||
|
||||
> Why? A bind call in the render path creates a brand new function on every single render.
|
||||
> Why? A bind call in the render path creates a brand new function on every single render. Do not use arrow functions in class fields, because it makes them [challenging to test and debug, and can negatively impact performance](https://medium.com/@charpeni/arrow-functions-in-class-properties-might-not-be-as-great-as-we-think-3b3551c440b1), and because conceptually, class fields are for data, not logic.
|
||||
|
||||
```jsx
|
||||
// bad
|
||||
@@ -564,6 +564,17 @@ We don’t recommend using indexes for keys if the order of items may change.
|
||||
}
|
||||
}
|
||||
|
||||
// very bad
|
||||
class extends React.Component {
|
||||
onClickDiv = () => {
|
||||
// do stuff
|
||||
}
|
||||
|
||||
render() {
|
||||
return <div onClick={this.onClickDiv} />
|
||||
}
|
||||
}
|
||||
|
||||
// good
|
||||
class extends React.Component {
|
||||
constructor(props) {
|
||||
|
||||
Reference in New Issue
Block a user