JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

State Management

react
mid
state-management

How do useSelector and useDispatch work in React-Redux?

redux
useSelector
useDispatch
state-management
hooks
Quick Answer

useSelector reads data from the Redux store by accepting a selector function that receives the entire state and returns the needed slice. useDispatch returns the store's dispatch function to send actions. Together they replace the older connect() HOC pattern with a simpler hooks-based API.

Detailed Explanation

React-Redux hooks provide a direct way to interact with the Redux store from functional components:

useSelector(selectorFn, equalityFn?)

  • Takes a selector function: (state) => state.someSlice
  • Re-renders the component when the selected value changes
  • Uses strict equality (===) by default to detect changes
  • For objects/arrays, use shallowEqual or memoized selectors (Reselect)
  • Runs the selector on every store update

useDispatch()

  • Returns the store's dispatch function
  • Use to dispatch actions: dispatch(actionCreator())
  • Reference is stable (doesn't change between renders)

vs connect() HOC: | Feature | Hooks | connect() | |---------|-------|-----------| | Syntax | Direct in component | Wraps component | | Memoization | Manual (useMemo) | Built-in (shallow compare) | | TypeScript | Better inference | Complex typing | | Testing | Needs Provider | Can test mapState separately |

Code Examples

useSelector and useDispatch basicsTSX
import { useSelector, useDispatch } from 'react-redux';
import type { RootState } from '../store';
import { increment, decrement } from '../features/counterSlice';

function Counter() {
  // Read from store
  const count = useSelector((state: RootState) => state.counter.value);
  const userName = useSelector((state: RootState) => state.user.name);

  // Get dispatch function
  const dispatch = useDispatch();

  return (
    <div>
      <p>{userName}'s count: {count}</p>
      <button onClick={() => dispatch(increment())}>+</button>
      <button onClick={() => dispatch(decrement())}>-</button>
    </div>
  );
}

Real-World Applications

Use Cases

Large-Scale State Management

Using useSelector/useDispatch for centralized state in enterprise applications with many developers

Optimistic UI Updates

Dispatching actions and using selectors to immediately reflect changes while syncing with server

Time-Travel Debugging

Leveraging Redux DevTools to replay actions and inspect state changes during debugging

Mini Projects

Redux Shopping Cart

intermediate

Build a full shopping cart with product listing, cart management, and checkout using Redux Toolkit

Real-Time Notifications

advanced

Implement a notification system with Redux middleware for WebSocket message handling

Industry Examples

WordPress Gutenberg

Uses Redux stores (@wordpress/data) for editor state management

Instagram Web

Uses Redux for global state management in their React application

Resources

React-Redux - useSelector

docs

Redux Toolkit - Quick Start

docs

Related Questions

What is Redux and Redux Thunk, and how do they manage application state and async operations?

senior
state-management

What is React Context and when should you use it?

mid
context
Previous
How do you integrate UI component libraries with React and when should you use them?
Next
How would you implement a useLocalStorage custom hook?
PrevNext