Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: does state have to be serializable? #2833

Closed
rogierschouten opened this issue Jan 9, 2015 · 2 comments
Closed

Question: does state have to be serializable? #2833

rogierschouten opened this issue Jan 9, 2015 · 2 comments

Comments

@rogierschouten
Copy link

According to this guide, all items in the state of a react component have to be serializable. I would like to store a non-plain javascript object, what would be the consequences?

@gaearon
Copy link
Collaborator

gaearon commented Jan 9, 2015

It's not a requirement, you can put anything in there. However generally it's a good idea to stick to plain objects or, for example, ImmutableJS types. What you usually don't want is having an object in state that is being mutated somewhere deeply inside, as you wouldn't be able to compare previous and next states, for example. It's easier to reason when state is immutable.

That being said, it's not a hard requirement. Put whatever you want there. If it has to be an object that is mutated elsewhere, note that React will have no way to know it changed unless you call setState again with the same object. Also note that you're missing out on potential effortless optimizations with PureRenderMixin if your state contents is mutable since state can no longer be shallowly compared.

@rogierschouten
Copy link
Author

That's very helpful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants