我正在使用withRouter()将位置和历史记录添加到我的反应视图组件中。在与页面交互时,我经常更改查询字符串,以便可以将视图添加为书签以供以后查看。例如,如果我点击一个国家,我可能会调用以下代码:
public onChangeCountry = (countryName: string): void => {
this.setState({country: countryName});
const search = `${queryString.stringify({
country: countryName
})}`;
const url = `/countries?${search}`;
this.props.history.replace(url);
}
但是,我注意到在更改国家/地区时,有些承诺会抛出“未安装的组件”异常:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
当查询字符串更改时,似乎正在卸载组件,然后重新安装,这会导致页面触发fetch()请求。
是否有一种方法可以防止仅在查询字符串更改时重新安装组件?