我需要使用 react-router 从服务器端的另一个页面重定向页面。我编写的代码在客户端工作,但不在服务器渲染中。你可以在这里找到代码:
这是/src/shared/components/LoginPage.js内的重定向代码:
componentWillMount() {
...
this.props.history.replaceState(null, '/home');
}
笔记:
我做了:
function requireAuth(nextState, replaceState) {
// replaceState({ nextPathname: nextState.location.pathname }, '/login');
}
...
<Route path="home" component={HomePage} onEnter={requireAuth} />
此代码有效,但我想在组件内进行重定向