Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add note about eslint:no-label #1009

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions content/docs/faq-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ handleSomething() {
}
```

One common pitfall when writing arrow functions is accidentally relying on labels and implicit returns:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this out as a 'rarely-used language feature called "labels"'

Copy link
Contributor Author

@swyxio swyxio Jun 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have strong feelings on this, whoever is merging or reviewing please feel free to change it or decide.


```js
// good
this.setState(({ bool }) => ({ bool: !bool }));

// bad
this.setState(({ bool }) => { bool: !bool }); // doesn't actually update state
```

This is because `bool:` in the second example is interpreted as a [label](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) instead of an object key, so the arrow function in the second example actually returns `undefined`. This may cause very confusing bugs, so we recommend using [eslint:no-labels](https://eslint.org/docs/rules/no-labels) to warn when this happen. This rule does come with [eslint-config-react-app](https://github.com/facebook/create-react-app/blob/d36603979591f37eed6f7a3efcba136e7547e75d/packages/eslint-config-react-app/index.js#L81) so if you use `create-react-app` you don't need to worry, but take note if you have your own custom ESLint setup.

[Learn more about setState](/docs/react-component.html#setstate)

### When is `setState` asynchronous?
Expand Down