Mr. Yusuf’s record merges with a change form at the counter through Object.assign and spread: which object changes, which drawer wins, and what becomes of the…
Why it matters
Merging objects is everywhere: updating React state, overriding default settings, applying a patch. Pick wrong and state is mutated silently (React does not re-render), or "why did the original change" because a box inside is shared.
The key idea
The only difference is the target: data itself, or a new object. Both are shallow.
Common mistakes
React does not re-render though the state looks changedObject.assign(state, patch) mutates the state itself and gives the same object back, so React sees the old reference. Use { ...state, ...patch }, which makes a new object.
Change a nested field in the copy, and the original changes too Assign and spread copy only one layer: a drawer holding an object copies the box’s number. Copy the layer you change too (alamat: { ...a.alamat }) or use structuredClone.
The merge comes out backwards: the changes are overwritten Order matters: the rightmost wins. { ...patch, ...base } lets base overwrite the patch; write { ...base, ...patch }.
Expecting a getter to be copied as a getter A getter on a source runs and its result is copied; a setter on Object.assign’s target is called. To copy the accessors themselves: Object.defineProperties(t, Object.getOwnPropertyDescriptors(s)).
Try it in this playground
Merge with Object.assign(data, …) Mr. Yusuf’s record itself changes: hasil === data.
Find a shared box hasil.alamat and perubahan.alamat, the same box number.
Reverse the order { ...perubahan, ...data }: data overwrites the changes.
Copy a getter What is copied is its value, not the getter.