React-redux context not found error: solve the problem when using useSelector hook
P粉427877676
P粉427877676 2023-09-14 00:09:19
0
1
635

I'm working on a React application using Redux to manage state. I've set up the Redux store and Provider in my index.js file:

// index.js

import { Provider } from 'react-redux';
import store from './store';

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>  
);

In my App component, I try to use react-redux's useSelector hook to access the store:

// App.jsx

import { useSelector } from 'react-redux';

function App() {

  const state = useSelector(state => state); // 报错

  // ...
}

However, this throws the following error:

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

I have confirmed:

  • store is correctly created and exported in store.js
  • App is the root component passed to Provider
  • I imported useSelector from 'react-redux'

But I still see this context not found error. What am I missing in the Provider settings to make the Redux store available to components using hooks like useSelector?

Any help is greatly appreciated!

P粉427877676
P粉427877676

reply all(1)
P粉432906880

Try passing context = {null} in the provider wrapper like below

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


 </Provider>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template