我作为一个初学者 React 开发者正在学习 redux-thunk 中间件,我不明白为什么函数(由 redux-thunk 返回)返回一个 Promise(由 fetch() 返回)
我尝试过不退回任何东西,结果成功了,那我们为什么要退回呢?
export function getCourses() {
return fetch(baseUrl)
.then(handleResponse)
.catch(handleError);
}
export function loadCourses() {
return function(dispatch) {
dispatch(beginApiCall());// dispatch some synchronous action
return courseApi
.getCourses().then(courses => {
dispatch(loadCourseSuccess(courses));
}).catch(error => {throw error;});
};
}
对于名为 MyComponent 的组件调度 loadCourses() 操作
function MyComponent(props){
.......
useEffect(() => {
loadCourses()
});
const mapDispatchToProps = {
loadCourses,
.....
}
}
最佳答案
我想我得到了答案(来自同事)。
如果您想链接某些事件,您的操作需要返回一个 Promise。
这只是在返回结果后允许链接事件的好方法!
关于javascript - 为什么 redux thunk 返回 Promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57645490/