【发布时间】:2017-09-29 17:32:34
【问题描述】:
我正在尝试在 Redux 中进行同步调度调用,其中第一次调用更新 isLoading 状态,以便我们可以在 UI 上显示加载图标,然后第二次调用在后台执行大量同步计算以更新项目位置 - 大约需要 2-5 秒。
第一个动作发生并且状态被正确更改,但似乎在那之后立即发生它并没有击中反应前端组件,而是进入第二个调度。当我在第一次调度后添加一个简短的timeout 时它可以工作,但我讨厌硬编码一个固定的等待时间。
这个问题有更好的解决方案吗?
不工作:
const showLoadingIcon = () => ({type: types.SHOW_LOADING_ICON});
export const updateInteractionTypeScores = (updatedValues) => dispatch => {
dispatch(showLoadingIcon());
dispatch({type: types.UPDATE_VALUES, updatedValues});
};
工作:
const showLoadingIcon = () => ({type: types.SHOW_LOADING_ICON});
export const updateInteractionTypeScores = (updatedValues) => dispatch => {
dispatch(showLoadingIcon());
setTimeout(() => dispatch({type: types.UPDATE_VALUES, updatedValues}), 100);
};
【问题讨论】:
标签: redux react-redux redux-thunk