页面不会在react-router-dom V4上再次渲染?

时间:2017-10-06 04:08:27

标签: reactjs react-router

我正在使用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>

因此,当转到 / user / 123 时,我可以获得用户页面但是如果在同一页面上转到 / user / 345 ,换句话说,当重定向到另一个只有新用户ID的链接,我无法获得新的用户页面,只保留上一页。

我有什么问题?

1 个答案:

答案 0 :(得分:0)

如果您导航到相同的路径路径,则反应将不会再次渲染 你可以在我的建议之下做到:

 componentWillReceiveProps(nextProps, nextState) {
    if (this.state.path != nextProps.location.pathname) {
        this.setState({
            path: nextProps.location.pathname
        }, () => {
            //process with your parameter here
        })
    }
}
相关问题