Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

useDispatch or useSelector throws: could not find react-redux context value

ErrorError: could not find react-redux context value; please ensure the component is wrapped in a <Provider>

Short answer

React Redux hooks read the store from React context. The error means no Provider sits above the component that called the hook. Wrap the app in a Provider with your store. In tests, render through a wrapper that adds one. In the Next.js App Router, put the Provider in a file marked use client.

  • Applies to react-redux 9, React 19, Next.js 15, Redux Toolkit 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find where the failing component is rendered, then check that a Provider with a store prop is one of its ancestors. In a test, the component is often rendered alone.

    grep -rn "<Provider" src app --include=*.js --include=*.jsx --include=*.ts --include=*.tsx
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on first render in the browser or on the device, in any component that calls useSelector, useDispatch or connect.No Provider wraps the component treeFix
    2. 2The app works, but the same component fails inside a Jest or Vitest test.A unit test renders the component without a ProviderFix
    3. 3The error appears in a Next.js app/ project, usually after adding Provider to app/layout.tsx directly.Next.js App Router: the Provider is rendered in a Server ComponentFix
    4. 4A Provider is present above the component, yet the error remains.Provider uses a custom context that the hooks do not useFix

What does the error look like?

  • Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>
  • Could not find "store" in the context of "Connect(MyComponent)"

Cause 1 of 4

No Provider wraps the component tree

1Check

Open the entry file (index.js, main.tsx or App.js) and look at what is rendered at the top. Hooks and connect get the store through React context, and Provider is what puts it there.

grep -rn "createRoot\|registerRootComponent\|AppRegistry" src index.js App.js 2>/dev/null

2Fix

Render Provider once, around the whole app, and pass it your store.

import { createRoot } from 'react-dom/client'
import { Provider } from 'react-redux'
import { store } from './store'
import { App } from './App'

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

3Verify

Reload the app. The component should render without the error.

npm run dev

Confirmed by redux.js.org

Did this fix it?

Cause 2 of 4

A unit test renders the component without a Provider

1Check

Look at how the test renders. A plain render(<Component />) from React Testing Library has no store above the component.

grep -rn "render(" src --include=*.test.* | head

2Fix

Write one custom render function that creates a fresh store per test and wraps the UI in a Provider, then use it in every test that needs Redux.

// utils/test-utils.tsx
import { render } from '@testing-library/react'
import { Provider } from 'react-redux'
import { setupStore } from '../app/store'

export function renderWithProviders(ui, { preloadedState = {}, store = setupStore(preloadedState), ...options } = {}) {
  const Wrapper = ({ children }) => <Provider store={store}>{children}</Provider>
  return { store, ...render(ui, { wrapper: Wrapper, ...options }) }
}

3Verify

Swap render for renderWithProviders in the failing test and run it.

npx vitest run   # or: npx jest

Confirmed by redux.js.org

Did this fix it?

Cause 3 of 4

Next.js App Router: the Provider is rendered in a Server Component

1Check

Layouts and pages are Server Components by default, and React context is not supported there. Check whether the file that creates the Provider has a use client line at the top.

head -n 3 app/providers.tsx app/layout.tsx

2Fix

Move the Provider into its own Client Component that accepts children, and render that from the layout. Any component that calls a Redux hook also needs use client.

// app/store-provider.tsx
'use client'
import { Provider } from 'react-redux'
import { store } from '../lib/store'

export default function StoreProvider({ children }: { children: React.ReactNode }) {
  return <Provider store={store}>{children}</Provider>
}

// app/layout.tsx
import StoreProvider from './store-provider'
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html><body><StoreProvider>{children}</StoreProvider></body></html>
  )
}

3Verify

Restart the dev server and open a route that reads from the store.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 4 of 4

Provider uses a custom context that the hooks do not use

1Check

Search for a context prop on Provider. If one is set, every connected component must use that same context.

grep -rn "context=" src | grep -i provider

2Fix

Remove the custom context unless you really need two stores. If you do, pass the same context to every connect call that belongs to that store.

<Provider store={store}>
  <App />
</Provider>

3Verify

Run the app again and open the screen that failed.

npm run dev

Confirmed by redux.js.org

Did this fix it?

How do I stop it coming back?

  • Keep one renderWithProviders helper and ban plain render in tests for connected components.
  • Mount Provider once, at the top of the entry file or one root layout component.
  • In Next.js, give every file that calls a Redux hook a use client line.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

next/link throws React.Children.only on its children

Error: React.Children.only expected to receive a single React element child.

React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.

3 causes, each with a fix

Web apps

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.