简体   繁体   English

我如何将 React Context 与 react-router-dom 一起使用?

[英]How can I use React Context with react-router-dom?

AuthContext:授权上下文:

import { createContext } from "react";
export const AuthContext = createContext(null);

App.js:应用程序.js:

const App = () => {
  const isLogin = false;

  if (isLogin) {
    return (
      <RouterProvider router={privateRoute} />
    );
  } else {
    return (
      <RouterProvider router={publicRoute} />
    );
  }
};

export default App;

I try to put <RouterProvider> in AuthContext like this:我尝试将<RouterProvider>放在AuthContext中,如下所示:

<AuthContext.RouterProvider router={privateRoute} />

and like this:像这样:

<AuthContext>
  <RouterProvider router={privateRoute} />
</AuthContext>

None of these options worked.这些选项都不起作用。 What am I doing wrong?我究竟做错了什么?

you are trying to use the AuthContext with the RouterProvider but AuthContext have no property or component called RouterProvider .您正在尝试将AuthContextRouterProvider一起使用,但AuthContext没有名为RouterProvider的属性或组件。

To use AuthContext in combination with the RouterProvider , you will need to wrap the RouterProvider component in a component that consumes the AuthContext 's value ( ie useContext(AuthContext) ).要将AuthContextRouterProvider结合使用,您需要将RouterProvider组件包装在一个使用AuthContext的值的组件中(即 useContext(AuthContext) )。

Here a snippet to help you out... In this we are using AuthContext value to detemine the value of routes and then use it with RouterProvider这里有一个片段可以帮助你......在这里我们使用AuthContext值来确定routes的值,然后将它与RouterProvider一起使用

 const App = () => { const auth = useContext(AuthContext); const routes = auth.isLoggedIn? privateRoute: publicRoute; return ( <RouterProvider router={routes} /> ); }; export default App;

inside and you need to wrap the App component inside AuthContext like...在里面,您需要将App组件包装在AuthContext ,例如...

ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <AuthContext>
      <App />
    </AuthContext>
  </React.StrictMode>
);

One has nothing to do with the other.一个与另一个无关。 The issue is that AuthContext doesn't have any RouterProvider property nor is the code you are trying to use actually using a React context Provider component and value.问题是AuthContext没有任何RouterProvider属性,您尝试使用的代码也没有实际使用 React context Provider组件和值。

Just wrap the App component with the AuthContext.Provider component and ensure a context value is passed.只需使用AuthContext.Provider组件包装App组件并确保传递上下文值。

Example:例子:

<AuthContext.Provider value={{ ..... }}>
  <App />
</AuthContext.Provider>

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

相关问题 如何避免 react-router-dom 中的重复名称? - How can I avoid duplication name in react-router-dom? 如何使用上下文 api 与 react-router-dom": "^5.2.0" 和 react": "^17.0.2" - How to use context api with react-router-dom": "^5.2.0" and react": "^17.0.2" 如何将 Jquery 插件与 React js 和 react-router-dom 一起使用 - How to use Jquery Plugin with React js and react-router-dom 如果视图在带有 react-router-dom 的 React 路由中,我如何取消重新渲染视图 - How can I cancel re-render view if view is in Route on React with react-router-dom 我如何在 react-router-dom 中像 react-navigation 一样获得“previous”? - How in react-router-dom can I get the `previous` as in react-navigation? 如何用 React 组件、react-router-dom 链接或锚标记替换位或字符串<a></a> - How can I replace bits or string with React component, react-router-dom Link or an anchor tag <a></a> 反应,我如何在 react-router-dom v6 的单个路由中渲染多个组件? - react , how can i render multiple components in single route in react-router-dom v6? 无法使用react-router-dom的WithRouter - Not able to use WithRouter of react-router-dom 如何使用<Prompt />在“react-router-dom”中:“^6.3.0” - How to use <Prompt /> in "react-router-dom": "^6.3.0" 如何正确使用 react-router-dom 中的 useHistory()? - How to properly use useHistory () from react-router-dom?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM