immerjs / immer

Create the next immutable state by mutating the current one
https://immerjs.github.io/immer/
MIT License
27.5k stars 850 forks source link

Maybe update docs #1123

Open olegdunkan opened 3 months ago

olegdunkan commented 3 months ago

🙋‍♂ Question


const a = { id: 1 };
const b = { id: 2, children: [a] };

const objs = { 1: a, 2: b };

const fn = produce((draft) => {
  draft[2].children[0].x = 1;
});

const r = fn(objs);

console.log(r[1]); // {id:1}
console.log(r[2].children[0]); //{id:1, x:1}

In two places of objs we have two pointers to the same object a but when we change it in one place we treat it like two different objects.

I expected that to be

console.log(r[1]); // {id:1, x:1}
console.log(r[2].children[0]); //{id:1, x:1}

I didn't find anything about that in docs.