setInadvancedconst state = { user: { address: { city: 'Madrid' } } }
const next = setIn(state, ['user', 'address', 'city'], 'Barcelona')
next.user.address.city // β 'Barcelona'
state.user.address.city // β 'Madrid' (unchanged)setIn eliminates boilerplate spread chains for deep updates while guaranteeing immutability at every level.
Updating deeply nested state immutably with spread operators can become verbose. A utility like setIn(obj, path, value) handles this recursively:
setIn(state, ['user', 'address', 'city'], 'Barcelona')
// β { ...state, user: { ...state.user, address: { ...state.user.address, city: 'Barcelona' } } }
Challenge: Implement setIn(obj, path, value) that returns a new object with the value set at the given key path, without mutating the original at any level.