Skip to content

Correct setState() usage in tutorial#13358

Closed
ludofischer wants to merge 1 commit into
react:masterfrom
ludofischer:patch-1
Closed

Correct setState() usage in tutorial#13358
ludofischer wants to merge 1 commit into
react:masterfrom
ludofischer:patch-1

Conversation

@ludofischer

@ludofischer ludofischer commented Apr 6, 2017

Copy link
Copy Markdown

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:

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

Motivation (required)

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.

Test Plan (required)

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

Next Steps

Sign the CLA, if you haven't already.

Small pull requests are much easier to review and more likely to get merged. Make sure the PR does only one thing, otherwise please split it.

Make sure all tests pass on both Travis and Circle CI. PRs that break tests are unlikely to be merged.

For more info, see the "Pull Requests" section of our "Contributing" guidelines.

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`.
@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. GH Review: review-needed Import Started This pull request has been imported. This does not imply the PR has been approved. and removed GH Review: review-needed labels Apr 6, 2017
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hramos has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@lumiasaki

Copy link
Copy Markdown

If my memory is correct, get value from this.state, modify it, then setState() with new value of state is safe in early version of React Native, but it is not safe anymore in the latest version of React Native, thanks for your updating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Import Started This pull request has been imported. This does not imply the PR has been approved.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants