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

Assigning a sub-reducer result to a property mutates original state #343

Closed
2 tasks done
renchap opened this issue Apr 4, 2019 · 4 comments
Closed
2 tasks done
Assignees

Comments

@renchap
Copy link
Contributor

renchap commented Apr 4, 2019

🐛 Bug Report

First of all, thanks for Immer!

I am using a main Redux state with child-states, each with its own reducer. For some actions I need the main reducer to update the whole state (for example initialisation from server data).

const subtreeReducer = produce((subtreeDraft, action) => {  }

const rootReducer = produce((draft, action) => {
  switch (action.type) {
    case "INIT_FROM_SERVER":
      return 
      break

    default:
     draft.subtree = subtreeReducer(draft.subtree, action)
  }
}, {)

I think I understand why this is not working (reassigning a whole property, not changing it), but I am not finding the proper "immer" way to do so, and I think this is a quite common usecase.

Link to repro (highly encouraged)

See Codesandbox: https://codesandbox.io/s/olo1yo9y3z

To Reproduce

See codesandbox

Expected behavior

I would expect the same result with a sub-reducer and without it. But when I am using my sub-reducer, the original state is mutated.
I guess the proper solution would be for the sub-reducer to not call produce, but it makes this reducer harder to test.

Environment

  • Immer version: 2.1.4
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@renchap renchap added the bug label Apr 4, 2019
@aleclarson aleclarson self-assigned this Apr 4, 2019
@renchap
Copy link
Contributor Author

renchap commented Apr 4, 2019

Note: The codesansbox is like this as I tried to reproduce a problem I am seeing in my app.

I just tested to remove the splice call it my reducer and the problem goes away. So I am really unsure of what is really happening here 🤔

@aleclarson
Copy link
Member

It's an internal bug. Looking into it. 👍

@aleclarson
Copy link
Member

🎉 This issue has been resolved in version 2.1.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@renchap
Copy link
Contributor Author

renchap commented Apr 4, 2019

Thanks, that was quick!
So this was not me doing anything wrong, good to know 😄

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

No branches or pull requests

2 participants