如何解决尝试从React Router v3迁移到v5时,无法弄清楚如何通过“ props.children”并在身份验证后重定向
我看到了很多示例,尝试了一些,但是无法确切地知道如何进行身份验证路由以及使用props.children。
该项目使用redux集成,路由如下所示:
const userIsAuthenticated = connectedReduxRedirect({
redirectPath: `\login`,redirectAction: routerActions.replace,wrapperDisplayName: 'UserIsAuthenticated'
});
const StandardAuthentication = userIsAuthenticated(
(props) => props.children
);
return(
<Provider store={store}>
<ConnectedRouter history={history}>
<Switch>
<Route
path='/register'
component={Register}
/>
<Route
path='/login'
render={() => (
<Login userManager={userManager} />
)}
/>
<Route
path='/callback'
render={() => (
<Callback userManager={userManager} />
)}
/>
//i am trying to change this part
<Route component={StandardAuthentication}>
<Route path="/" component={App}>
...many sub routes
</Route>
</Route>
<Route path="*" component={NotFoundExternal}/>
</Switch>
</Router>
</Provider>)
我根据评论添加了以下内容,并从其他来源进行了一些修改:
const AuthenticationRoute = ({ component:Component,...rest}) => {
return (
<Route
{...rest}
render={(props) => {
<StandardAuthentication>
<Component {...props} />
</StandardAuthentication>;
}}
/>
);
};
,路线为:
<AuthenticationRoute path="/" component={App}/>
并将子路线移至App
它似乎不起作用。
它会一直执行到回调页面,并且不会转到“身份验证”路由或应用程序,而是会到达“未找到”路由。 有人可以指导我如何解决此问题吗?
预先感谢
解决方法
我不确定,但可能是这项工作。您没有正确调用组件:
const AuthenticationRoute = ({ component:Component,...rest}) => {
return (
<Route
{...rest}
render={props => <StandardAuthentication {...props}/>
}
/>
);
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。