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

Redux store.getState is not immutable #1739

Closed
natehardy opened this issue May 17, 2016 · 3 comments
Closed

Redux store.getState is not immutable #1739

natehardy opened this issue May 17, 2016 · 3 comments

Comments

@natehardy
Copy link

I love redux and was a little surprised to see that getState or accessing any part of the state doesn't return a copy of the state. Is there a reason not to return Object.assign({}, "store.state")?

@smashercosmo
Copy link

smashercosmo commented May 17, 2016

@PartyHardy I think, that the main reason is performance. Copying object every time you want to get your state is obviously not a very good practice. And even if you copy it, it won't be deeply immutable. And deep copying is even less performant. So, if you're worrying about suddenly mutating your state, just deep freeze it in dev mode or use libraries like immutable.js or seamless-immutable, that will throw an error on attempt to mutate state.

@gaearon
Copy link
Contributor

gaearon commented May 17, 2016

@smashercosmo is correct. Object.assign() is shallow so it only copies one level deep. Copying deeper comes with a big perf penalty and disables any optimizations that we can get from comparing the references to check if something changed. We also don’t want to use something like deep-freeze by default because it also has performance penalties and impacts correctness (we can’t just enable it in DEV and disable in PROD because the behavior would be different).

@gaearon gaearon closed this as completed May 17, 2016
@markerikson
Copy link
Contributor

There's also some dev tools that will warn you if you're mutating. See the DevTools#Linting section of my Redux addons catalog.

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

4 participants