【发布时间】:2017-05-21 13:03:03
【问题描述】:
我计划在我的 redux 操作创建器中使用 axios 进行异步调用和承诺。目前还没有可以使用的 API,所以我将内容保存在本地。
如何在反应组件中使以下内容可链接?
动作创作者
export function fetchResultsIfNeeded() {
return (dispatch, getState) => {
if (true) {
return dispatch(fetchResults(day, hash));
}
else {return Promise.resolve();}
};
}
export function fetchResults(day, hash) {
return (dispatch) => {
// Dispatch something here
return new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
// Do some stuff here
// Dispatch some other stuff here as well
}); // Want to chain on this promise in react component
};
}
反应组件
import { fetchResultsIfNeeded } from 'actions';
// Class based react component
checkResults(periodFrom, periodTo) {
this.props.fetchResultsIfNeeded([periodFrom, periodTo]).then(() => {/* Do some stuff here */});
}
// Connected via react redux
export default connect(null, { fetchResultsIfNeeded })(component);
目前收到如下错误,莫非还在等待react-redux左右?
Uncaught TypeError: Cannot read property 'then' of undefined
将动作创建者和组件放在 Pen 中(不知道如何显示完整代码)
【问题讨论】:
-
这对我来说是正确的。您可能忘记安装
redux-thunk中间件了吗? -
@Jacob 不,在这个应用程序上使用了一段时间 :)
-
你能发布一个更完整的 React 组件示例吗?也许问题出在某个地方。
-
@Jacob 查看更新的问题;)
-
确保 fetchResults() 中的 then() 返回一个 promise
标签: javascript reactjs redux