REDUX-如何导入返回Promise的API函数,并将其添加到动作创建者的有效负载中



我的Redux已经生疏了,至少可以说,我正在尝试从返回Promise的API导入函数。我想把这个Promise添加到动作创建者的有效负载中,但它抱怨TypeError: Cannot read property 'getBetList' of undefined

这是我的动作创造者:

import { getBetList, getLatestPrices } from '../api/index';
export const GET_BET_LIST = 'GET_BET_LIST';
export const GET_LATEST_PRICES = 'GET_LATEST_PRICES';

export function fetchBetList() {
const response = this.getBetList();
return {
type: GET_BET_LIST,
payload: response
}
}
export function fetchLatestPrices() {
const response = this.getLatestPrices();
return {
type: GET_LATEST_PRICES,
payload: response
}
}

我假设从API收到的Promise将在有效负载上可用,将流经redux-Promise,最后我得到了我需要的数据,但它停止了,因为它无法读取getBetList?我在进口方面哪里做错了?

您应该使用getBetList&getLatestPrices,因为您在文件顶部导入了它。从下面的行中删除this将修复您的错误。

const response = this.getBetList();
^^^^

这里也是

const response = this.getLatestPrices();

顺便说一句,它也不起作用——你应该使用异步流:

export function fetchLatestPrices() {
return (dispatch) => {
getLatestPrices()
.then(response => dispatch({type: GET_LATEST_PRICES, payload: response}));
}
}

关于async actions的更多信息

最新更新