I'm using React 17.0.2, react-router-dom 6.0.2 in my app right now.
I try to use react Context and Route to add authenticate on routes.
In App.js I can get value from AuthContext
...
const { authState, authReducer } = useContext(AuthContext);
<Routes>
<Route element={PrivateRoute}>
<Route path="/dashboard/*" element={<MainLayout />} />
</Route>
<Route>
<Route path="/auth/*" element={<EmptyLayout />} />
</Route>
<Route path="*" element={NotFound} />
</Routes>
...
but in PrivateRoute I got undefined
const {authState, authReducer} = useContext(AuthContext);
const {state, dispatch} = authState;
const ele = state.login === true ? <Outlet /> : <Navigate to="/login" replace />;
return <Route path={path} element={ele} />;
In this case, how could I get AuthContext in PrivateRoute?
Thank you