我有这个组件,我将 this.props.route 更改为通过 redux 重定向。在这种情况下,我从 '/aaa' 重定向到 '/bbb' 并且它没有重定向页面。
我通过在代码中添加 console.log 来测试 this.props.route 是否得到更改,并且 props 正在更新,但 Redirect 似乎不起作用。
基本上,组件 {aaa} 从 redux 更新 this.state.route。Redux 将状态传播到 prop。因此, RedirectHandler 组件被重新渲染,但 Redirect 没有触发。
这行不通。
重定向处理器组件
render() {
console.log('rerender', this.props.route); 1.) '/aaa' 2.) '/bbb'
return(
{ this.props.isAuthenticated && <Redirect to={this.props.route} /> }
<Route exact path="/" component={aaa} />
<Route exact path="/aaa" component={aaa} />
<Route exact path="/bbb" component={bbb} />
)
}
虽然这行得通,
render() {
return(
{ this.props.isAuthenticated && this.props.route === '/bbb'
&& <Redirect to={this.props.route} /> }
{ this.props.isAuthenticated && <Redirect to={this.props.route} /> }
<Route exact path="/" component={aaa} />
<Route exact path="/aaa" component={aaa} />
<Route exact path="/bbb" component={bbb} />
)
}
问题是,如果 props.route 正在更新,为什么它没有将我重定向到指定的路线?
<Redirect to={this.props.route} />
或者
为什么第一个块不起作用而第二个块起作用?:/