我在React钩子中使用了多个上下文和约简器。
这是我的App.js文件
import React from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import Map from "./components/Map";
import Header from "./components/Header";
import Signup from "./components/Auth/Signup";
import Login from "./components/Auth/Login";
import UserList from "./components/User/UserList";
import "mapbox-gl/dist/mapbox-gl.css";
import { ApolloProvider } from "react-apollo";
import { ApolloClient } from "apollo-client";
import { WebSocketLink } from "apollo-link-ws";
import { InMemoryCache } from "apollo-cache-inmemory";
import { default as keysConfig } from "./keys.config";
import AuthProvider from "./providers/authProvider";
import withRoot from "./providers/withRoot";
import PageContent from "./pages/pageContent";
import ProtectedRoute from "./routePermissions/ProtectedRoute";
import AdminRoute from "./routePermissions/AdminRoute";
import PinProvider from "./providers/pinProvider";
import UserProvider from "./providers/userProvider";
const wsLink = new WebSocketLink({
uri: `${keysConfig.WssBaseUrl}/graphql`,
options: {
reconnect: true
}
});
const client = new ApolloClient({
link: wsLink,
cache: new InMemoryCache()
});
const App = () => {
return (
<Router>
<ApolloProvider client={client}>
<AuthProvider>
<PageContent>
<Header />
<Switch>
<PinProvider>
<ProtectedRoute exact path="/" component={Map} />
</PinProvider>
<Route exact path="/login" component={Login} />
<Route exact path="/signup" component={Signup} />
<UserProvider>
<AdminRoute exact path="/userlist" component={UserList} />
</UserProvider>
</Switch>
</PageContent>
</AuthProvider>
</ApolloProvider>
</Router>
);
};
export default withRoot(App);
我正在使用多个上下文和简化器。它使我使用多个提供程序。但是我想使用提供者来指定路由。
当我像代码片段一样使用Route时,ProtectedRoute不起作用。
我希望有人能帮助我解决这个问题。 您可以为我提供一个更好的方法,该方法如何更有效地将多个上下文和化简器组合到react挂钩中。
答案 0 :(得分:1)
<Switch>
适用于一级孩子。不管是<Route>
还是<Provider>
。因此,您需要调整组件内部的代码移动提供程序,或者仅利用render()
的{{1}}变体:
Route
应该工作正常