Correct `setState()` usage in tutorial

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 https://github.com/facebook/react-native/pull/13358

Differential Revision: D4852404

Pulled By: hramos

fbshipit-source-id: 834759e16bcfbd5a8de71bf0c56f2b154f3321e1
This commit is contained in:
Ludovico Fischer 2017-04-07 13:23:54 -07:00 committed by Facebook Github Bot
parent 909af08f24
commit 29404f087e
1 changed files with 3 additions and 1 deletions

View File

@ -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);
}