【发布时间】:2026-01-30 04:15:02
【问题描述】:
我想使用一些数据时遇到问题(但数组是空的,redux 有信息)我认为这是一个异步问题。 首先,我在
中获取了我的数据 componentDidMount() {
this.props.getFaq();
}
我希望结果映射它。
为了首先获取数据,我使用了这段代码
import axios from "axios";
import { GET_FAQ, GET_ERRORS } from "./types";
export const getFaq = () => dispatch => {
axios
.get("/faqs")
.then(res =>
dispatch({
type: GET_FAQ,
payload: res.data
})
)
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err
})
);
};
然后我把它改成
import axios from "axios";
import { GET_FAQ, GET_ERRORS } from "./types";
export const getFaq = () => async dispatch => {
const payload = await getData();
dispatch({
type: GET_FAQ,
payload
});
};
async function getData() {
const result = await axios("/faqs");
console.log(result.data);
return await result.data;
}
但是当我控制台记录它时数据仍然是空的
【问题讨论】:
-
你在你的 redux store 中使用 redux-thunk 吗?
-
是的,正在使用它
标签: javascript reactjs redux async-await axios