reactjs - 当用户在具有不同查询参数的相同基本 url 上时,BrowserHistory 不起作用
问题描述
当我使用相同的基本 URL 但具有不同的查询参数时,BrowserHistory 不起作用。
// currently i am on same url but different query param's.
**settings/organization/appointment-manager?from=2020-04-04T09:58:29.761Z&to=2020-04-04T09:58:29.761Z&pageNo=1&pageSize=30&activeKey=3**
import { browserHistory } from 'react-router';
import moment from 'moment-timezone';
refresh(){
// I want to call componentDidMount again when i push the bellow URL.
const startDate = moment();
const endDate = moment();
browserHistory.push(`/settings/organization/appointment-manager?from=${startDate}&to=${endDate}&pageNo=1&pageSize=30&activeKey=3`);
}
当 refresh() 调用我的 URL 时更改了新参数,但没有调用 componentDidMount()。
当我在具有不同查询参数的同一个基本 URL 上时,如何再次调用 componentDidMount()。
解决方案
你应该在这里做的是你应该使用componentWillRecieveProps()
. 因为你所做的只是更新router
道具。
所以尝试做这个:
componentWillReceiveProps(newProps) {
if (newProps.location.search !== this.props.location.search) {
//search params have changed
}
}
或者对于更新版本的 React
getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.location.search !== this.props.location.search) {
//search params have changed
}
}
推荐阅读
- android - 从 Activity 导航到 Fragment
- python-3.x - dynamoDB上的动态扫描操作
- ios - 将 FirebaseUI 与 Twitter 登录一起使用(iOS 应用)
- c++ - UE4 沿其他样条移动样条
- javascript - jQuery 验证规则没有功能
- m - Azure LogAnalytics 范围 KQL
- laravel - 在多个分叉存储库之间共享 Laravel Eloquent 模型是可能的吗?
- angular - 单击复选框时要选择当前页面记录,例如只有 10 条记录
- docker - docker 上的解析服务器,使用 https
- reactjs - 扩展使用 withTranslation() 的反应类组件