Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 41 ยท Redux with React

Provider and the React-Redux Hooks

Connect Redux to React with react-redux: wrap your app in Provider, read state with useSelector and dispatch actions with useDispatch. Build a shared cart in a live example.

Redux itself knows nothing about React. The react-redux package is the official bridge. It has one component and a few hooks, and they are all you need for almost every app:

API Role
<Provider store={store}> Makes the store available to every component below it.
useSelector(selector) Reads a value from the state and re-renders the component when that value changes.
useDispatch() Returns the store's dispatch function.
useStore() Returns the store itself (rarely needed).

react-redux cannot be loaded in our editor, so the real setup code is read-only. Live examples start with a short block marked STAND-IN that implements createStore, Provider, useSelector and useDispatch with React's own useContext and useSyncExternalStore (the same primitive react-redux 9 builds on). Everything under the marker is code you would write unchanged in a real project.

Provider

Provider puts the store into React context. Place it once, at the top of the tree, so that every component can reach the store without props.

JSX
// main.jsx (needs react-redux and @reduxjs/toolkit; read-only here)
import { createRoot } from 'react-dom/client';
import { Provider } from 'react-redux';
import { store } from './app/store';
import App from './App';

createRoot(document.getElementById('root')).render(
  <Provider store={store}>
    <App />
  </Provider>
);

A component outside the Provider that calls useSelector throws an error saying it could not find the react-redux context. You will provoke that error on purpose below.

useSelector

useSelector takes a selector function: it receives the whole state and returns the part you want. React-Redux runs it after every dispatch and compares the new result with the previous one using ===. If they differ, your component renders again; if they are the same, it does not.

JSX
import { useSelector } from 'react-redux';

function CartBadge() {
  const count = useSelector((state) => state.cart.items.length);
  return <span className="badge">{count}</span>;
}

Select the smallest value you need. useSelector in Depth covers equality, derived data and performance.

useDispatch

useDispatch returns dispatch. Call it in event handlers (and effects) with an action:

JSX
import { useDispatch } from 'react-redux';
import { added } from './cartSlice';

function AddButton({ product }) {
  const dispatch = useDispatch();
  return <button onClick={() => dispatch(added(product.id))}>Add</button>;
}

More patterns for dispatching are in useDispatch and Events.

A shared cart

Three components that know nothing about each other share one cart: a header badge, a product list and a cart panel. None of them passes props or callbacks to another, and each re-renders only when the value it selected changes (the "renders" counters show it).

Header, products and cart share one store

Add a few items and watch the counters. ProductList only dispatches and selects nothing, so it renders once and never again. Header selects a number and re-renders only when that number changes. CartPanel selects the qty object, which is a new object after every cart action, so it re-renders each time, which is correct here because its content changes.

What happens under the hood

sequenceDiagram
    participant C as Component
    participant H as useSelector
    participant S as Store
    C->>H: render: run selector(state)
    H->>S: subscribe(listener)
    C->>S: dispatch(action) from a handler
    S->>S: reducer computes new state
    S-->>H: notify subscribers
    H->>H: run selector again, compare with ===
    H-->>C: re-render only if the result changed

Each useSelector registers a subscription. After every dispatch it re-runs its selector and asks React to render only when the value changed.

Using the Provider correctly

The Provider must wrap every component that reads or writes the store. This example places a component outside it and catches the error:

What happens outside the Provider

If you ever see that message in a real app, check that <Provider> wraps the component in question, including components rendered through portals or separate roots (a second createRoot needs its own Provider) and in tests (wrap the component under test, covered in the Testing chapter).

Good habits

  • Create the store in its own module (app/store.js) and import it only in main.jsx; components use hooks, never the store object directly.
  • Put the Provider above the router and any other providers that need Redux data.
  • Call hooks at the top level of components, like all hooks; never inside loops or conditions.
  • Prefer many small useSelector calls to one big one that returns most of the state.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Provider do?

    Provider takes a store prop and puts it into React context. Hooks below it find the store there.
  2. 2. When does a component using useSelector re-render?

    React-Redux re-runs the selector after each dispatch and compares results; no change means no render caused by the store.
  3. 3. Which hook gives you the function to send actions?

    useDispatch returns the store's dispatch function.
  4. 4. ProductList in the example only dispatches. How often does it re-render because of store updates?

    Without a selector it has no subscription to state, so store changes do not re-render it.

Summary

  • react-redux connects a store to React with Provider, useSelector, useDispatch (and useStore).
  • Wrap the app once in <Provider store={store}>; components outside it cannot use the hooks.
  • useSelector subscribes to the store and re-renders only when its selected value changes by ===.
  • useDispatch returns dispatch; components that only dispatch never re-render from store changes.
  • Select small values and keep hooks at the top level of components.

Next: useSelector in Depth