Skip to content

Commit

Permalink
Correct setState() usage in tutorial
Browse files Browse the repository at this point in the history
Summary:
If the new state depends on the previous state, if I remember correctly, it’s safer to use  `setState()` with a function argument to ensure we’re not reading from an outdated `state`.

Thanks for submitting a PR! Please read these instructions carefully:

- [x] Explain the **motivation** for making this change.
- [x] Provide a **test plan** demonstrating that the code is solid.
- [x] Match the **code formatting** of the rest of the codebase.
- [x] Target the `master` branch, NOT a "stable" branch.

The tutorial suggests to use `setState()` with an object argument when the new state depends on the previous state. In such situations, it’s preferable to use a function to ensure the previous state is up-to-date.

Updates documentation only, so there are no additional tests. Rendering the site.

Sign the [CLA][2], if you haven't already.

Small pull requests are much easier to review and more likely to get merged. Make sure th
Closes #13358

Differential Revision: D4852404

Pulled By: hramos

fbshipit-source-id: 834759e16bcfbd5a8de71bf0c56f2b154f3321e1
  • Loading branch information
Ludovico Fischer authored and facebook-github-bot committed Apr 7, 2017
1 parent 909af08 commit 29404f0
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/State.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class Blink extends Component {
// Toggle the state every second
setInterval(() => {
this.setState({ showText: !this.state.showText });
this.setState(previousState => {
return { showText: !previousState.showText };
});
}, 1000);
}
Expand Down

0 comments on commit 29404f0

Please sign in to comment.