简体   繁体   English

尝试从 React 路由器 v3 迁移到 v5,无法弄清楚如何通过“props.children”并在身份验证后重定向

[英]Trying to migrate from React router v3 to v5 , Unable to figure out how to pass 'props.children' and redirect after authentication

The project uses redux integration,initial routes look something like this:该项目使用 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>)

i added the following as per the comment and a bit of modification from some other source:我根据评论添加了以下内容,并从其他来源进行了一些修改:

 const AuthenticationRoute = ({ component:Component, ...rest}) => {
        return (
            <Route
                {...rest}
                render={(props) => {
                    <StandardAuthentication>
                        <Component {...props} />
                    </StandardAuthentication>;
                }}
            />
        );
    };

and the route as:路线为:

<AuthenticationRoute path="/" component={App}/> 

and moved the sub routes to App并将子路由移至 App

It doesnt seem to work.它似乎不起作用。

It goes till callbackpage and the doesnt go to the 'Authentication' route or App but hits the "NotFound" route.它一直持续到回调页面,并且 go 没有到达“身份验证”路由或应用程序,但命中“未找到”路由。 Could anybody guide me how to fix this?有人可以指导我如何解决这个问题吗?

Thanks in Advance提前致谢

I am not sure but might be this work.我不确定,但可能是这项工作。 You are not calling component right:您没有正确调用组件:

const AuthenticationRoute = ({ component:Component, ...rest}) => {
        return (
            <Route
                {...rest}
                render={props => <StandardAuthentication {...props}/>
                }
            />
        );
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM