LearnThatStack Ace your next interview
Redux · question
Question 30 of 84

How do you handle complex state updates with nested objects in RTK?

intermediate
← All Redux questions
Re-explain

With Redux Toolkit you update nested data by assigning to it directly, like state.profile.address.city = action.payload. createSlice runs reducers through Immer, which turns those writes into an immutable update and copies only the objects on the changed path.

Common patterns:

  • Replace a nested object by assigning a new value to its property, as updateAddress does below.
  • Update one deep field by assigning to that field, as updateCity does.
  • Update an item in an array by finding it first and then changing it, like state.items.find(item => item.id === id).

Arrays also support push and index assignment directly on the draft.

Two rules prevent bugs. Either mutate the draft or return a new state, never both in one reducer. And never reassign state itself, because that only changes the local variable.

For data nested many levels deep, normalizing it by ID keeps reducers flat. You can still return a new state object when that logic reads more clearly.

const userSlice = createSlice({
  name: 'user',
  initialState: {
    profile: {
      name: '',
      address: {
        street: '',
        city: ''
      }
    }
  },
  reducers: {
    updateAddress: (state, action) => {
      // Direct assignment works with Immer
      state.profile.address = action.payload;
    },
    updateCity: (state, action) => {
      state.profile.address.city = action.payload;
    }
  }
});
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Redux cheatsheet.

← Back to all Redux questions
Pro · $10/mo

68 of 84 Redux answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime