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

'Cannot apply patch, path doesn't resolve' when modifying property of an object in Map

AhsanSarwar45 opened this issue Β· comments

πŸ› Bug Report

applyPatches throws error Cannot apply patch, path doesn't resolve: map/1/b when applying inversePatches generated by the following produce call:

interface Test {
  a: number;
  b: string;
}

let state = {
  map: new Map<number, Test>([])
};

state.map.set(1, { a: 5, b: "hello" }); // Setting initial state

let inversePatches: Patch[] = [];

state = produce(
    state,
    (draftState) => {
      const test = draftState.map.get(1);
      if (test) test.b = "world";
    },
    (_patches, _inversePatches) => {
      inversePatches = _inversePatches;
    }
);

It works fine when the patch op is add instead of replace:

state = produce(
    state,
    (draftState) => {
      const test = draftState.map.get(1);
      if (test) draftState.map.set(1, {...test, b: "world"}); // works
    },
    (_patches, _inversePatches) => {
      inversePatches = _inversePatches;
    }
);

Link to repro

https://codesandbox.io/s/awesome-stitch-xxtxq3?file=/src/index.ts

  • Click Set Map Property button to set map property
  • Click Undo button to call applyPatches with inversePatches

To Reproduce

Steps to reproduce the behavior:

  • Call enableMapSet and enablePatches
  • Create a Map with an object as the value type and add some key-value pairs to it
  • Use produce to mutate the Map
  • Inside produce, retrieve an element using Map.get() and modify a property on the element
  • Save the inversePatches generated by produce
  • Call applyPatches(map, inversePatches)

Observed behavior

applyPatches throws error Cannot apply patch, path doesn't resolve: map/1/b.

Expected behavior

The inversePatches should be applied successfully and state should revert back to the state it was in before the produce call

Environment

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

Getting the same error. Any updates on this?

:tada: This issue has been resolved in version 9.0.20 :tada:

The release is available on:

Your semantic-release bot πŸ“¦πŸš€