immerjs / immer

Create the next immutable state by mutating the current one

Home Page:https://immerjs.github.io/immer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

original object unexpectly frozen in some condition

wighawag opened this issue Β· comments

πŸ› Bug Report

immer will freeze the original object if no modification are made to a draft.

This is unexpected and caused me lots of frustration to figure why my object got frozen

Link to repro

Reprodiction repo : https://github.com/bug-reproduction/immer-freeze

on Codesandbox: https://codesandbox.io/p/github/bug-reproduction/immer-freeze/main

To Reproduce

pnpm test

Observed behavior

If you create a draft and modify that draft to finally finish the draft, the original remains unchanged

but if you create a draft and finish it without doing any changes, the original object is frozen

Expected behavior

I expect the behaviot of immer to remains the same whether modification to the draft are made or not. And more importantly I expect he original object to not be modified in any way (freezing it is a modification that can affect any function using it)

Environment

  • Immer version: 10.0.3
  • [X ] I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)

There are two invariants at conflict here:

  1. The output is always frozen
  2. The input is never modified

When input === output one of those two has to be violated. Immer choses by default to keep guaranteeing 1, because a) it detects developer errors early (the mental model is that trying to mutate the output of produce is a mistake by the developer!), and b) because it gives better performance downstream (further produces are more efficient if the input is immutable).

However, if you don't like that, the behavior can be disabled as documented: https://immerjs.github.io/immer/freezing