useDispatch() Error: Could not find react-redux context value; please ensure the component is wrapped in a

前端 未结 3 875
鱼传尺愫
鱼传尺愫 2021-02-18 13:54

I am trygin to use React-Redux library and I am getting the error on the title. I wrapped my components with Provider but I still get the error, only if I implement the useDispa

3条回答
  •  感情败类
    2021-02-18 14:40

    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 (
         // Set context
           // Now App has access to context
        
      )
    }
    
    const App = () => {
      const dispatch = useDispatch(); // Works!
    ...
    

提交回复
热议问题