React-router 查询参数发生变化,不触发reload?

#1

我通过如下代码的addQuery方法来更新查询参数:

import { hashHistory } from 'react-router';

/**
 * @param {Object} query
 */
export const addQuery = (query) => {
    const location = Object.assign({}, hashHistory.getCurrentLocation());
    Object.assign(location.query, query);
    hashHistory.push(location);
} ;

/**
 * @param {...String} queryNames
 */
export const removeQuery = (...queryNames) => {
    const location = Object.assign({}, hashHistory.getCurrentLocation());
    queryNames.forEach(q => delete location.query[q]);
    hashHistory.push(location);
} ;


addQuery( { id: 1 } )执行完,查询参数更新成功,url变成了/patch?id=1 ;
然后
addQuery( { id: 2 } )执行完,查询参数更新成功,url变成了/patch?id=2 ;

然后我通过浏览器的后退按钮,url是回退到了/patch?id=1,可是视图的数据还是id为2的结果。

我期望的是id为1的数据,需要怎么操作?

我的react-router版本( 3.0.0 )

在github上看到类似问题:
1、View not updating on parameters change, only if reloaded
2、params prop does not reflect a change when redirecting to url with same route pattern but different url params

#2

this.props.location.query.id 的改变,componentWillReceiveProps就能检测到,然后修改再请求修改state即可

#3

componentWillReceiveProps是可以监听params的改变,但仅是当前的params,点击回退之后新的params并不能读取到。

#4

好的,谢谢。

#5

可以的吧,可能我没明白你的意思,componentWillReceiveProps里面当前location的query可以用this.props.location获取到啊;将要跳转过去的参数,可以用componentWillReceiveProps的参数nextProps获取到的吧?

#6

找的资料都是推荐componentWillReceiveProps的方法,感觉麻烦,如果react-router底层保证能省好多力,好几个页面都得添加componentWillReceiveProps了。。这样子的话