使用新版本 6 的 react-router 编写 ProtectedRoute 的正确方法是什么?我写了这个,但这不是一条路线
const PrivateRoute = ({ component: Component, ...props }) => {
if (!Component) return null;
return props.isAuthenticated
? <Component />
: <Navigate to={props.redirectLink} /> }
export default PrivateRoute;
原文由 Victor 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是我使用 useRoutes 实现私有路由的工作示例。
应用程序.js
路由.js