我需要使用我的应用程序url,并根据我的应用程序当前状态的变化做出react .
基本上,我有一个组件,需要在每次url更改时更新它的状态.状态是通过redux的props 传递给装饰师的,就像这样
@connect(state => ({
campaigngroups: state.jobresults.campaigngroups,
error: state.jobresults.error,
loading: state.jobresults.loading
}))
目前,我正在使用componentWillReceiveProps生命周期方法来响应来自react-router的url更改,因为当url在此过程中发生更改时,react-router将向处理程序传递新的props .props .params和这个.props .查询——这种方法的主要问题是,我在这个方法中启动了一个操作来更新状态——然后它会go 传递新的props ,组件将再次触发相同的生命周期方法——所以基本上创建了一个无休止的循环,目前我正在设置一个状态变量来阻止这种情况发生.
componentWillReceiveProps(nextProps) {
if (this.state.shouldupdate) {
let { slug } = nextProps.params;
let { citizenships, discipline, workright, location } = nextProps.query;
const params = { slug, discipline, workright, location };
let filters = this._getFilters(params);
// set the state accroding to the filters in the url
this._setState(params);
// trigger the action to refill the stores
this.actions.loadCampaignGroups(filters);
}
}
是否有一种基于路由转换触发操作的标准方法,或者我是否可以将存储的状态直接连接到组件的状态,而不是通过props 传递?我曾try 使用WillTransition静态方法,但我没有访问该方法的权限.props .派过go .