Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

React 16.5.0: It is not recommended to assign props directly to state #545

Closed
asapach opened this issue Sep 6, 2018 · 16 comments
Closed

Comments

@asapach
Copy link

asapach commented Sep 6, 2018

After upgrading to React 16.5.0 I now see the following warning:

Warning: Provider: It is not recommended to assign props directly to state because updates to props won't be reflected in state. In most cases, it is better to use props directly.

This seems to be caused by this line:

this.state = props || {}

Relevant changes in React: facebook/react#11593 and facebook/react#11658

@mweststrate
Copy link
Member

mweststrate commented Sep 6, 2018 via email

@asapach
Copy link
Author

asapach commented Sep 6, 2018

Here's the sandbox: https://codesandbox.io/s/z241341v9l?expanddevtools=1
Example taken from the readme.

@yuminjustin
Copy link

I also met today.

package versions:
image

It's came from Provider,but it doesn't affect the project running
image

@urugator
Copy link
Contributor

urugator commented Sep 7, 2018

I think we just have to change

// https://github.com/mobxjs/mobx-react/blob/master/src/Provider.js#L18
this.state = props || {}
// to => 
this.state = { ...props }

I quess the only reason why we use state is the warning in getDerivedStateFromProps right?

@ematipico
Copy link

But that's a shallow copy. A full copy should be made.

@urugator
Copy link
Contributor

urugator commented Sep 7, 2018

Why? The copy is just to satisfy react's check (it only compares references):
https://github.com/facebook/react/pull/11658/files#diff-b69c8f2165f95ef2355ac661c12fa407R679

@mkhuda
Copy link

mkhuda commented Sep 7, 2018

This also happened on https://github.com/zeit/next.js/tree/canary/examples/with-mobx (newest NextJS with Mobx store)

@mweststrate
Copy link
Member

mweststrate commented Sep 7, 2018 via email

@gaearon
Copy link

gaearon commented Sep 8, 2018

Since you're handling that manually later this.state = {...props} is fine.

Avoid writing code like this in your apps though, it's usually a bug. :-)

@mweststrate
Copy link
Member

mweststrate commented Sep 10, 2018

@gaearon

Edit totally misread the comment :)

@mweststrate
Copy link
Member

Fixed and releases as 5.2.8!

@inoyakaigor
Copy link

inoyakaigor commented Sep 10, 2018

@mweststrate why do you change ...spread by Object.assign?

@mweststrate
Copy link
Member

mweststrate commented Sep 10, 2018 via email

aballman pushed a commit to codeamp/panel that referenced this issue Sep 10, 2018
aballman added a commit to codeamp/panel that referenced this issue Sep 12, 2018
* Update React and Apollo Packages.

* Updated Apollo file to handle errors

* Updated mobx-react to 5.2.8 mobxjs/mobx-react#545

* Updated Mobx Dependency in package.json
@billysin
Copy link

Thanks mweststrate

@FishPlusOrange
Copy link

awesome 💯

@danielkcz
Copy link
Contributor

@FishPlusOrange Seriously? Please refrain from commenting on old and closed issues, especially if you only want to express gratitude...

@mobxjs mobxjs locked as resolved and limited conversation to collaborators Aug 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests