页面在react-router-dom V4上没有再次渲染?
问题描述:
我正在使用react-router-dom v4。 这是我的路由器代码片段。页面在react-router-dom V4上没有再次渲染?
import {BrowserRouter as Router, Route, BroswserHistory} from 'react-router-dom';
<Router>
<Main>
<route exact path="/" component={All}/>
...
<route path="/user/:id" component={User}/>
</Main>
</Router>
因此,当去到/用户/ 123我能得到用户的页面,但如果去/用户/ 345在同一页上,换句话说,当重定向到具有唯一的又一个新的链接用户名,我不能得到新的用户页面只有它保持前一页。
我怎么了?
答
如果您导航到同一条路线的路径,反应就不会再呈现
U可以做以下我的建议:
componentWillReceiveProps(nextProps, nextState) {
if (this.state.path != nextProps.location.pathname) {
this.setState({
path: nextProps.location.pathname
},() => {
//process with your parameter here
})
}
}
与“withRouter”包装你组分反应的路由器-DOM和尝试。像'export withRouter(YourComponent)''一样 –