私のアプリでは、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
クライアントでも:
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,
);
});
ルート.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
と、コンソールに警告が表示され、アプリが壊れます。
私が間違っていることは何か提案はありますか? ありがとう!