更改了URL中的参数之后,该页面将再次重新安装

问题描述 投票:0回答:1

我在页面上有一些过滤器,应用后,我想将这些过滤器值存储在带有参数的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
    })
  });
};
reactjs react-router
1个回答
0
投票

使用v5的最新版本的react-router-dom,行为已更改。即使在参数或查询更新时,也将重新安装组件,而不是重新渲染

在这种情况下,查询参数或url参数只需要在componentDidMount中处理,而不是getDerivedStateFromProps/componentDidUpdatecomponentDidMount的组合实现

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