我试图创建一个 <PrivateRoute>
如使用 TypeScript 的 react-router 文档 中所述。谁能帮我吗?
react-router 文档中的 privateRoute:
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
fakeAuth.isAuthenticated ? (
<Component {...props}/>
) : (
<Redirect to={{pathname: '/login', state: { from: props.location }
}}/>
)
)}/>
)
下面是我的 TypeScript 版本(它不会工作):
const PrivateRoute = (theProps: { path: string, component: React.SFC<RouteComponentProps<any> | undefined> | React.ComponentClass<RouteComponentProps<any> | undefined> }) => {
return <Route path={theProps.path} render={props => (
fakeAuth.isAuthenticated ? (
<React.Component {...theProps} /> <!-- **** It will raise error *** -->
) : (
<Redirect to={{
pathname: '/',
state: { from: props.location }
}} />
)
)} />
}
<React.Component {...thisProps} />
不对。错误是:NodeInvocationException:inst.render 不是函数 TypeError:inst.render 不是函数
原文由 Charlie 发布,翻译遵循 CC BY-SA 4.0 许可协议
原始答案(2017)
(以下更新答案)
错误可能与输入和渲染中的隐式返回有关。当你解决这个问题时,你最终会得到这样的东西:
这个组件可以这样使用:
上述解决方案有一些缺点。其中之一是您失去了类型安全性。
可能扩展
Route
组件是更好的主意。所以你可以像这样使用组件:
更新(2019 年 11 月)
如果您更喜欢编写函数式组件,您可以以非常相似的方式来完成。这也适用于 React Router 5:
更新(2019 年 12 月)
如果要将用户重定向到用户首先要访问的路径,则需要记住该路径,以便在认证成功后进行重定向。以下答案将指导您完成:
使用 react-router-dom 成功认证后将用户重定向到他们请求的页面
更新(2021 年 3 月)
上面的解决方案有点过时了。 ProtectedRoute 组件可以简单地写成如下:
如果您使用 React Router V6,您需要将
Redirect
替换为Navigate
。可以在此处找到重定向到最初请求页面的完整示例:更新(2022 年 1 月)
作为
<Routes>
的孩子需要是<Route>
元素<ProtectedRoute>
可以更改为:<ProtectedRoute>
现在可以如下应用:我还更新了 React Router 6 示例。到目前为止,甚至还有关于此的官方指南: https ://reactrouter.com/docs/en/v6/examples/auth