【发布时间】:2021-11-08 08:13:58
【问题描述】:
我对 Redux 有点了解,但我不明白如何将函数放入函数中。
import api from '../utils/api';
import {
GET_PROFILE, PROFILE_ERROR
} from './types';
export const getCurrentProfile = () => async (dispatch) => {
try {
const res = await api.get('/profile/me');
dispatch({
type: GET_PROFILE,
payload: res.data
});
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
}
};
我对这条线有疑问:
export const getCurrentProfile = () => **async (dispatch) => {}**
- 我们是否在此处使用
async (dispatch) => {}定义自己的函数? - 我们为什么要定义自己的函数?
- 我知道
dispatch做了什么,但我们从哪里得到它,为什么要在这两个对象上使用它? - 此模式的名称是什么?
【问题讨论】:
-
对于名称,通常称为curried function。
-
Redux 中为什么会出现这种模式,主要是enable async redux action function, which is called redux thunks。
-
@EmileBergeron 谢谢你太棒了。!!!我在 Node.js 和 React 中都需要这个
标签: javascript reactjs redux