Method to turn off freezing when using immer with useSelector()? #1189
-
Is there a method to allow data being read from redux with useSelector to not be wrapped in an immer wrapper? The behavior it offers in the reducers is welcome but I'm noticing that since you can't add properties to frozen objects, developers will often use nested spreads in their views before dispatching an action. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
There's a couple different answers here. First, Immer technically does export a way to disable freezing, with However, I'd strongly recommend leaving that as-is. It not only catches bugs and prevents accidental mutations, the dev mode middleware rely on checking for frozen state as a bailout signal to speed up those checks. Beyond that, it sounds like the frozen state is actually doing its job here. Developers shouldn't be "adding properties to objects", because those are the original objects from state. Adding properties to those objects would be an actual mutation, which is wrong. It also sounds like this situation involves devs "calculating new state" first, and putting that new state into the action. Instead, we recommend letting the reducer do all that new state calculation work, and keeping the action as minimal as possible: |
Beta Was this translation helpful? Give feedback.
There's a couple different answers here.
First, Immer technically does export a way to disable freezing, with
setAutoFreeze
: https://immerjs.github.io/immer/freezingHowever, I'd strongly recommend leaving that as-is. It not only catches bugs and prevents accidental mutations, the dev mode middleware rely on checking for frozen state as a bailout signal to speed up those checks.
Beyond that, it sounds like the frozen state is actually doing its job here. Developers shouldn't be "adding properties to objects", because those are the original objects from state. Adding properties to those objects would be an actual mutation, which is wrong.
It also sounds like this situation involves devs "c…