Idea With Immer we can deeply clone an object and modify the property. Handy for a state update in React. Installation npm i immer Without immer
const abcde = { a: 'a', b: { c: 'c', d: { e: 'e' } } }
const abcdef = { ...abcde, b: { ...abcde.b, d: { ...abcde.b.d, f: 'f' } } }
// { a: 'a', b: { c: 'c', d: { e: 'e' , f: 'f' } } }
With immer
import { produce } from 'immer'
const abcde = { a: 'a', b: { c: 'c', d: { e: 'e' } } }
const abcdef = produce(abcde, (obj) => {
obj.b.d.f = 'f'
})
// { a: 'a', b: { c: 'c', d: { e: 'e' , f: 'f' } } }