Skip to content

Conversation

@mmcgahan
Copy link
Contributor

@mmcgahan mmcgahan commented Sep 4, 2018

Followup from tweet about setState updater docs, with support from @gaearon:

Possible confusion in React docs for setState updater pattern - calling first arg prevState seems odd because it could actually be a future value of state relative to this.state. Should it just be called state and align with getDerivedStateFromProps args?

https://twitter.com/mikemcgahan/status/1037074327269568512

The reference to 'previous state' is ambiguous in the 'updater' context, so this PR changes the suggested prevState arg name to state. This change helps conceptually align the 'updater' function with the behavior of getDerivedStateFromProps - both are pure functions that take a 'current' state arg and return partialState updates.

@reactjs-bot
Copy link

Deploy preview for reactjs ready!

Built with commit b0a6a53

https://deploy-preview-1155--reactjs.netlify.com

@gaearon gaearon merged commit cf62830 into reactjs:master Sep 4, 2018
@karlli1976
Copy link

It's glad to know I'm not alone :)

Actually I found the code on Try It On CodePen still uses prevState, which causes me a bit confused: according to the context, shouldn't it be state?

And I found if I change prevState to prevMyState, the code still works.

this.setState(prevMyState => ({
      showWarning: !prevMyState.showWarning
    }));

@mmcgahan
Copy link
Contributor Author

@karlli1976 yeah, you can technically name it whatever you want - it won't break the code, but it may mislead people who read prevState as referring to some sort of cached or 'past' state. I can't update the CodePen code, but it's probably not critical

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants