【发布时间】:2020-05-07 08:34:36
【问题描述】:
我在页面上有一些过滤器,应用后我想将这些过滤器值存储在带有查询参数的 URL 中。但是每当我更改参数时,它会再次重新安装页面并导致componentDidMount 再次调用。我想在道具发生变化时收到getDerivedStateFromProps:static 的通知。
我用这个函数来更新我的查询参数。示例网址https://qa.portal.tech/feeds?country=USA&page=1&pageSize=10&price=yes
export const updateQueryString = (history: History, queryParams: any) => {
const {
replace,
location: { pathname }
} = history;
replace({
pathname,
search: queryString.stringify({
...queryParams
})
});
};
【问题讨论】:
-
你可以使用history.push来更新而不是替换
-
我也试过了,但要重新安装 happing
标签: reactjs react-router