【发布时间】:2019-03-09 08:37:46
【问题描述】:
我有一个页面可以验证用户并在验证成功时将他们重定向到登录页面。但是,使用 redirect 的 URL 会发生变化,但不会呈现 Login 的新页面。如何让新页面呈现?
App.js
var child;
render() {
if (this.state.toLogin && !this.state.mounted) {
<Route exact path="/Login" component={Login} />;
<Route exact strict path="/" render={({location}) => {
if (location.pathname === window.location.pathname) {
return <Redirect to="/Login"/>;
}
return null;
}}
return(
<div className="App">
<ReactCSSTransitionGroup
transitionName="remove"
transitionEnterTimeout={100}
transitionLeaveTimeout={100}>
{child}
</ReactCSSTransitionGroup>
</div>
);
}
索引.js
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
编辑:根据答案进行了更改,但现在 URL 没有改变,页面也没有呈现
【问题讨论】:
-
stackblitz.com/edit/react-router-starter-rb2cvc?file=index.js 这按预期工作,你能说明你的问题是什么吗?
-
如何为
var child赋值?你能展示App的完整代码库吗? -
查看我的答案@black
-
事实证明我没有正确渲染东西,但感谢所有答案
-
如果您因为试图重定向到外部页面而来到这个问题,那么这个问题对我有帮助:stackoverflow.com/questions/42914666/react-router-external-link/…
标签: javascript reactjs react-router react-router-dom