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

Immer does not work properly with arrow function getter

NEWESTERS opened this issue Β· comments

πŸ› Bug Report

If getter in class is arrow function (or bind in constructor), it's result is not handled with immer

Link to repro

https://codesandbox.io/s/immer-arrow-bug-usfy6z

To Reproduce

Steps to reproduce the behavior:

import { produce, immerable } from "immer";

class Dictionary<K extends string, V> {
  [immerable] = true;

  private _data: Record<K, V>;

  public constructor(data: Record<K, V>) {
    this._data = data;
  }

  public get = (key: K): V | undefined => { // <-- Note arrow function here
    return this._data[key];
  };
}

const dictionary1 = new Dictionary({ "1": ["foo"] });

const dictionary2 = produce(dictionary1, (draft) => {
  draft.get("1")?.push("bar");
});

console.log(dictionary1.get("1")); // --> ["foo", "bar"]
console.log(dictionary2.get("1")); // --> ["foo", "bar"]

Observed behavior

Prints

["foo", "bar"] // <-- original mutated
["foo", "bar"]

Expected behavior

Prints

["foo"] // <-- original not mutated
["foo", "bar"]

Environment

We only accept bug reports against the latest Immer version.

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