useDispatch() Error: Could not find react-redux context value; please ensure the component is wrapped in a <Provider>
App
must be wrapped in provider since you are using useDispatch
in it. Right now it's just a child. Provider
sets the context so only its children can have access to it, not a parent.
One solution would be to create a wrapper component for it:
const AppWrapper = () => {
const store = createStore(rootReducer);
return (
<Provider store={store}> // Set context
<App /> // Now App has access to context
</Provider>
)
}
const App = () => {
const dispatch = useDispatch(); // Works!
...