JS Built-ins
redux-toolkitcreateSliceintermediate

Nested State with Immer

πŸ“– Usage Example
reducers: {
  setTheme: (state, action) => {
    state.user.preferences.theme = action.payload
  },
}

Immer lets you write deeply nested mutations naturally β€” no need to spread every level of the state tree.

Nested State with Immer

Immer handles deeply nested state mutations transparently. You can assign to nested properties just like plain JavaScript.

Challenge: Create a profile slice with nested user.preferences.theme and write reducers to update the name and theme.

profileSlice.reducer(undefined, setTheme('dark'))
// β†’ { user: { name: '', preferences: { theme: 'dark' } } }
πŸ’‘ Hints (3)
  • β€’ Immer works at any nesting depth: `state.user.preferences.theme = action.payload` is safe.
  • β€’ Only the properties you touch are updated β€” the rest of the state is preserved.
  • β€’ You don't need to spread nested objects β€” Immer handles structural sharing automatically.