01 / 14

What is a slice in Redux Toolkit?

In Redux Toolkit, a slice is a collection of reducer logic and actions for a single feature of your application. It combines the state, action creators, and reducers for that feature into a single, easy-to-manage unit.

When you define a slice, you provide an object to `createSlice` function with three main properties:
  1. 1

    name: A string name for the slice that is used as a prefix for the generated action types (e.g., counter/increment).

  2. 2

    initialState: The starting value for that specific piece of the Redux state.

  3. 3

    reducers: An object where the keys are the names of the actions (like increment or decrement) and the values are functions that define how the state should change.

Key Features of a Slice
  1. 1

    Defines a portion of the Redux state (e.g., user, counter, todos). Ideally this portion of the state represents state for a feature.

  2. 2

    Automatically generates action creators and action types based on reducers.

  3. 3

    Simplifies reducer logic by allowing direct state mutation using Immer.

  4. 4

    Keeps related logic (actions + reducers) together for better organization.

Example: Creating a Slice
Difficulty: 5/10
Topics: createSlice, reducers & actions, state organization

Scenario Questions

0-2 years experience
  1. 1

    Imagine you're adding a new feature to a small React app that uses Redux Toolkit. How would you create a slice to manage the loading state for an API call, and which files would you modify?

  2. 2

    If you dispatch an action generated by a slice but the state doesn't update, what are the most common reasons in RTK's createSlice implementation?

2-5 years experience
  1. 1

    You're refactoring a medium‑sized codebase and notice duplicated reducer logic across several slices. How would you consolidate that logic using createSlice, and what trade‑offs should you consider?

  2. 2

    During debugging you see a slice's reducer being called but the UI doesn't reflect the change. Walk me through the steps you would take to isolate the problem.

  3. 3

    Explain how you would handle async logic like fetching posts within a slice using createAsyncThunk, and why you might keep the thunk separate from the slice definition.

5-8 years experience
  1. 1

    In a large application with many feature modules, how do you decide the granularity of slices, and what impact does that have on store size and code‑splitting?

  2. 2

    Suppose two slices need to share some state but you want to avoid circular dependencies. How would you structure the slices or use extraReducers to achieve this safely?

  3. 3

    If adding several new slices leads to noticeable performance degradation due to frequent re‑renders, what slice‑level strategies would you employ to mitigate the issue?

8+ years experience
  1. 1

    Your organization is migrating a legacy Redux codebase to Redux Toolkit. What architectural guidelines would you set for slice organization, naming conventions, and versioning to ensure long‑term maintainability across teams?

  2. 2

    When multiple teams own different slices that need to be combined into a single store, how would you design store initialization and slice registration to support independent deployment and hot‑module replacement?

  3. 3

    Discuss the trade‑offs of colocating slice logic with UI components versus keeping slices in a central domain layer in a micro‑frontend architecture.

Follow-up Questions

  • Can you sketch the code you would write for that slice?
  • What would you check first if dispatching the slice's action didn't change the UI?
  • How do you normally test a slice's reducers and generated actions?