反应路由器改变到当前页面与不同的参数不起作用

问题描述 投票:2回答:2

我有一些主要的路线作为我的基本页面,我用GET的参数更改内容,就像在post.html?pnum=8中使用以下代码:

browserHistory = ReactRouter.browserHistory;
browserHistory.push("post.html?pnum=" + '8');

我有以下路线:

<Router history={browserHistory}>
  <Route path="/" component={RouteA} />
  <Route path="index.html" component={RouteA} />
  <Route path="me.html" component={RouteB} />
  <Route path="post.html" component={RouteC} />
  <Route path="tag.html" component={RouteD} />
  <Route path="basket.html" component={RouteE} />
  <Route path="*" component={vars.currentRoute} />
</Router>

问题是,当我改变路线时,它工作得恰到好处,但是当我只更改browserHistory的参数时,路径会发生变化,但页面上没有任何变化。

我想这样做:例如:当前路径是:"post.html?pnum=" + '8'我想把它改成这个"post.html?pnum=" + '3'

javascript reactjs react-router
2个回答
1
投票

如果它是您尝试访问的相同组件,但只有使用不同的参数,这将导致您只更改页面内容,请考虑更改组件的state。就像你的组件显示基于你的state的元素。如果我没有误解,改变state将有助于你的目的。


1
投票

你需要倾听路径变化

componentWillReceiveProps(nextProps) {
    if( this.props.location.search !== nextProps.location.search) {
       // fetch new data...
    }

然后组件将重新渲染。

© www.soinside.com 2019 - 2024. All rights reserved.