07 / 14

How do you access a slice’s state in a component using?

In Redux Toolkit, the useSelector and useDispatch hook from React-Redux allows you to read data and dispatch actions of a specific slice of the Redux store inside a React component.

Steps to Access Slice State
  1. 1

    Import the useSelector or useDispatch hook from react-redux.

  2. 2

    Use it inside your component to access the slice’s state or dispatch from event handlers and functions.

  3. 3

    Reference the slice key you defined in configureStore() (e.g., state.counter.value, dipatch(increment())).

Example: Accessing Slice State with useSelector
Difficulty: 6/10
Topics: useSelector, state normalization, memoization

Scenario Questions

0-2 years experience
  1. 1

    You're trying to display a user's name from the auth slice in a component, but it keeps re-rendering even when the name hasn't changed. How would you fix that?

  2. 2

    Your component is breaking because useSelector is returning undefined. What are two common reasons this could happen when accessing a slice?

2-5 years experience
  1. 1

    A feature shows a list of orders and their total prices, but the UI lags when scrolling. You suspect the selector is recalculating too often. How would you investigate and fix it?

  2. 2

    A teammate says their component isn't updating when the cart slice changes. You check the selector and see it's returning a new array every time. What’s likely wrong, and how do you fix it?

5-8 years experience
  1. 1

    You're optimizing a dashboard with 10+ components reading from the same large state slice. How would you structure your selectors to minimize re-renders and avoid memory bloat?

  2. 2

    A legacy feature uses deeply nested state, and selectors are becoming unmaintainable. How would you refactor this to improve performance and testability without breaking existing components?

8+ years experience
  1. 1

    We're migrating from Redux to RTK Query, but several components still rely on manual useSelector calls to legacy slices. How do you plan the deprecation strategy while ensuring zero regressions?

  2. 2

    A cross-team feature shares a state slice across three micro-frontends. How would you design the state structure and selector layer to avoid coupling, enable independent deployments, and maintain performance at scale?

Follow-up Questions

  • What happens if you pass a plain object directly to useSelector without memoizing?
  • How would you debug a component re-rendering even when the slice data hasn't changed?
  • Why might you choose to normalize state instead of nesting it deeply?