我在页面上有一些过滤器,应用后,我想将这些过滤器值存储在带有参数的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
})
});
};
使用v5的最新版本的react-router-dom,行为已更改。即使在参数或查询更新时,也将重新安装组件,而不是重新渲染
在这种情况下,查询参数或url参数只需要在componentDidMount
中处理,而不是getDerivedStateFromProps/componentDidUpdate
和componentDidMount
的组合实现