【发布时间】:2018-01-19 14:57:13
【问题描述】:
我使用 react、redux 和 redux-thunk middlewere。
我的代码如下:
function thunkFunction(){
return(dispatch) => {
dispatch(toggleSmth(false));
dispatch(toggle2Smth(false));
SomeService.getSmth()
.then(response => {
dispatch(oneMoreAction);
dispatch(one2MoreAction);
}
}
}
现在我在某个组件中的 componenDidMount() 之后调度整个 thunk 动作。
它是这样工作的:
- 调度(toggleSmth(false));
- 调度(toggle2Smth(false));
- 承诺回调队列(不做 .then 元素)
- } 转到右大括号
- 组件重新渲染
- 去.then(
- 调度(oneMoreAction)
- 自动组件重新渲染(为什么!?)
- 回到第二次调度 -> 调度(one2MoreAction)
- 另一个自动组件重新渲染(为什么!?)
问题是:
为什么一旦调度等待整个堆栈清除。而在 Promise 中 dispatch 组件会自动重新渲染,然后返回到下一个 Promise 函数元素?
此外,当我像这样将 dispatch(toggleSmth(false)) 放在 promise 中时:
function thunkFunction(){=
return(dispatch) => {
SomeService.getSmth()
.then(response => {
dispatch(toggleSmth(false));
dispatch(toggle2Smth(false));
dispatch(oneMoreAction);
dispatch(one2MoreAction);
}
}
}
dispatch(toggleSmth(false)) 和 dispatch(toggle2Smth(false)) 自动重新渲染组件。 (不等待清除回调堆栈'}')。在重新渲染之后 -> 到第一个 dispatch(oneMoreAction);
为什么一旦调度导致自动重新渲染,而你必须等待函数内部元素的执行?
【问题讨论】:
-
但是一旦它像你说的那样工作,一旦没有
标签: reactjs redux react-redux redux-thunk