简体   繁体   English

使用 useContext 钩子获取“错误:找不到 react-redux 上下文值;请确保组件包装在<provider> "</provider>

[英]using useContext hook get "Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>"

I am using useContext for my react app, I wrap my parent component with provider and in child component I call useContext to get access to global context and also I use useDispatch to dispatch the functions within the context.我在我的反应应用程序中使用useContext ,我用提供者包装我的父组件,在子组件中我调用 useContext 来访问全局上下文,并且我使用useDispatch在上下文中调度函数。

export const QuotesContext = React.createContext<QuoteContextInterface>({});
export const QuotesProvider = QuotesContext.Provider;

const ParentComponent = ({}) => {

return (
<QuotesProvider value={{getUsers, users}} >
  <ChildComponent />
</QuotesProvider>
)
}


const ChildComponet = ({}) => {
  const quoteContext = useContext(QuotesContext);
  const dispatch = useDispatch();

useEffect(() => {

  dispatch(quoteContext.getUsers)

 }, [])
}

}

the error I get is Error: Uncaught [Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>]我得到的错误是Error: Uncaught [Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>] Error: Uncaught [Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>]

also in the error path I see that its is complaining about useDispatch line in child component.同样在错误路径中,我看到它在抱怨子组件中的 useDispatch 行。

I will appreciate your help我会感谢你的帮助

I think you are importing useDispatch from react-redux , while context has nothing to do with this.我认为您正在从react-redux导入useDispatch ,而上下文与此无关。 To change the state of context, your context needs to have it's own state (with useReducer or useState ).要更改上下文的 state,您的上下文需要有自己的 state(使用useReduceruseState )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:找不到 react-redux 上下文值; 请确保组件被包裹在一个<Provider>在使用 useselector 时 - Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider> while using useselector 错误:找不到 react-redux 上下文值; 请确保组件被包裹在一个<provider> : Redux 与 NextJS</provider> - Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>: Redux with NextJS 错误:找不到 react-redux 上下文值; 请确保组件被包裹在一个<provider></provider> - Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider> 我的测试失败了,因为“找不到 react-redux 上下文值; 请确保组件被包裹在一个<Provider> ” - My tests are failing because of “could not find react-redux context value; please ensure the component is wrapped in a <Provider>” 开玩笑说:“找不到 react-redux 上下文值;请确保组件包装在<provider> "</provider> - In jest : "could not find react-redux context value; please ensure the component is wrapped in a <Provider>" Webpack5 模块联合:“未捕获的错误:找不到 react-redux 上下文值; 请确保组件被包裹在一个<provider> ”</provider> - Webpack5 Module Federation: “Uncaught Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>” 找不到 react-redux 上下文值错误随机触发 - could not find react-redux context value error triggering randomly Redux 工具包和 React Native。 错误:找不到 react-redux 上下文值; - Redux ToolKit and React Native. Error: could not find react-redux context value; 开玩笑测试 redux 连接的组件给出错误“请确保组件包裹在<provider> ”</provider> - Jest testing redux connected component gives error “ please ensure the component is wrapped in a <Provider>” 提供者react-redux中的错误 - Error in Provider react-redux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM