繁体   English   中英

react-router v2-在onEnter中替换不起作用

[英]react-router v2 - replace in onEnter doesn't work

在我的应用程序中,我正在使用react-router v2- https://github.com/reactjs/react-router

我在onEnter重定向存在问题:

一些组成部分

static async onEnter({ flux }, nextState, replace, callback) {
    const token = flux.getStore('auth').getAccessToken();
    if (!token) {
        replace('/login');
        return callback();
    }
}

我正在使用服务器端渲染并使用了本指南-https: //github.com/reactjs/react-router/blob/master/docs/guides/ServerRendering.md#async-routes,因为它说我需要使用match在客户端上进行match

client.js

RouterMatch({history: browserHistory, routes: patchedRouteHooks(routes)}, (error, redirectLocation, renderProps) => {
    // renderProps are `undefined` when replace from onEnter hook
    ReactDOM.render(
        <Router {...renderProps} createElement={passFluxToComponent} />,
        mountNode,
    );
});

routes.js

const routes = (
    <Route path="/" component={AppHandler}>
        <Route path="/" component={AuthHandler}>
            <Route path="login(/)" component={LoginFormHandler} />
            <Route path="signup(/)" component={SignupFormHandler} />
            <Route path="resetpassword(/)" component={ResetPasswordFormHandler} />
            <Route path="changepassword(/)" component={ChangePasswordFormHandler} />
            <Route path="logout(/)" component={LogoutHandler} />
        </Route>

        <Route path="/" component={AppLayoutHandler}>
            <Route path="/" component={EnsureAuthHandler}>
                <Route path="me/favorites(/)" component={FavoritePropertiesHandler} />
                <Route path="me/settings(/)" component={UserSettingsHandler} />
                <Route path="dashboard(/)(:status)(/)" component={DashboardFlowHandler} />
                <IndexRoute component={DashboardFlowHandler} />
            </Route>

            <Route path="/s(/)" component={SearchResultsHandler} />
            <Route path="/user/:id(/)" component={UserDetailsHandler} />
            <IndexRoute component={EnsureAuthHandler} />
        </Route>

        <IndexRoute component={AppLayoutHandler} />
    </Route>
);

export default routes

patchRouteHooks.js

function patchRouteHooks (Route, patchData = {}) {
    const { props: { children, component } } = Route;

    function _patchChildren (children) {
        if (Array.isArray(children)) {
            return React.Children.map(children, ChildRoute => patchRouteHooks(ChildRoute, patchData));
        } else {
            return patchRouteHooks(children, patchData);
        }
    }

    return {
        ...Route,
        props: {
            ...Route.props,
            onEnter: component && component.onEnter && function (...args) {
                component.onEnter.call(null, patchData, ...args);
            },
            onLeave: component && component.onLeave && function (...args) {
                component.onLeave.call(null, patchData, ...args);
            },
            children: children ? _patchChildren(children) : null
        }
    };
}

export default patchRouteHooks;

在onEnter中进行替换后,我收到的renderProps undefined ,这会在控制台和损坏的应用程序中导致警告:

在此处输入图片说明

有什么建议我做错了吗? 谢谢!

如您所说,renderProps是undefined

我没有传递renderProps,而是做了大致与您一样的事情: ReactDOM.render( <Router history={browserHistory} routes ={patchedRouteHooks(routes)} createElement={passFluxToComponent} />, mountNode, );

我无法完全理解匹配是异步执行的,并且没有一个功能完善的应用程序(带有服务器渲染),我只是停止使用它了:)

您可以保留当前的实现(与match匹配),并将后备渲染包装在

if (redirectLocation) { ...my proposition } else { ...your implementation }

暂无
暂无

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

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