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

Initial state immutability #313

Closed
ivi-hamiti opened this issue Feb 6, 2019 · 6 comments · Fixed by #417
Closed

Initial state immutability #313

ivi-hamiti opened this issue Feb 6, 2019 · 6 comments · Fixed by #417

Comments

@ivi-hamiti
Copy link

I am currently exploring immer for a use case of improving an component performance with immutability and angular OnPush change detection. I encountered a strange thing with the auto freezing (which by documentation is as expected) and wanted to know if this is a normal approach or i am pushing to make something work which is not the approach expected by immer.

So i have a complex component state (deeply nested). The sub properties of the object are not created as immutable using the produce but by simple object notation. When produce is called on the state, and the property values are set, immer will only freeze the setter/getter but not the value (which is an object).

I wanted to know if this is something that can be improved by immer or i am tackling this problem wrong and should create every object as immerable or using produce?

@aleclarson
Copy link
Member

aleclarson commented Feb 6, 2019

Please provide some example code to better illustrate what you expect versus the current behavior. Thanks!

@ivi-hamiti
Copy link
Author

ivi-hamiti commented Feb 11, 2019

So taking the following example

const state = { propA: {}, propB: {} };
const mutated = produce(state, draft => {});

And supose propA is a deeply nested property which is created prior to state initialization and as there are no changes to the state or properties propA will not be frozen.

So i would expect that the produced state propA would have been frozen.

@mweststrate
Copy link
Collaborator

mweststrate commented Feb 11, 2019 via email

@chenhx2015
Copy link

I want to submit the same feature request too, then i found your discussion. Are there any simple and
cheap way to meet the requirement?
Current, i am using Object.freeze(a new object) in the produce function to ensure the Initial state immutable.

@aleclarson
Copy link
Member

@chenhx2015 See here: #260 (comment)

@immerjs immerjs locked as resolved and limited conversation to collaborators Jul 10, 2019
@aleclarson
Copy link
Member

🎉 This issue has been resolved in version 4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants