【发布时间】:2017-05-06 21:22:59
【问题描述】:
我正在使用 react-redux,并在我的 redux 状态中添加了一个“过滤器”字段,其中包含我想要用于从我的数据库接收数据的所有过滤器。
我的目标是在添加过滤器后立即使用更新的过滤器从 db 接收数据。这是我到目前为止得到的:
--- actions.js ---
...
export function addFilter(filter, value) {
return {
type: 'ADD_FILTER',
filter: filter,
value: value
}
}
...
--- reducer.js ---
...
case 'ADD_FILTER':
return update(state, {
filters: {
$merge: {
[action.filter]: action.value
}
}
});
...
--- 过滤组件.js ---
...
addFilterAndUpdateData(filter, value) {
this.props.addFilter(filter, value);
this.props.getData(this.props.filters);
// this.props.filters is connected to state.filters.
// it DOES update, but not right away. so when getData is dispatched
// this.props.filters holds the old object and not the updated one
}
...
添加过滤器后,我想立即调度 getData 操作,以便从数据库中接收带有最新过滤器的数据。
问题是 this.props.filters 尚未更新。 有没有办法“等待”更新?
到目前为止我想出的最好的解决方案是使用 componentWillReceiveProps,但是我必须添加条件(因为我不一定要在每次我的 props 更改时调度 getData,只有当它是作为过滤器的结果时更新)
这里正确的“react-redux”方法是什么?
【问题讨论】:
标签: reactjs redux react-redux