【问题标题】:Function parameter explanation in Redux where a function returns a function? [duplicate]Redux中函数返回函数的函数参数解释? [复制]
【发布时间】: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) => {}**
  1. 我们是否在此处使用 async (dispatch) => {} 定义自己的函数?
  2. 我们为什么要定义自己的函数?
  3. 我知道dispatch 做了什么,但我们从哪里得到它,为什么要在这两个对象上使用它?
  4. 此模式的名称是什么?

【问题讨论】:

标签: javascript reactjs redux


【解决方案1】:
  1. 是的。 getCurrentProfile 返回一个带有一个参数的新函数。

  2. 也许他们会在闭包中使用该函数。闭包可用于隐藏信息。

  3. 无论使用什么你创建的函数,都会在调用它时将dispatch 传递给该函数。

【讨论】:

  • 非常感谢@Alex。你当然是对的,它解决了我的问题!
  • @RidwanBinMonjur 欢迎您。请把我的答案标记为正确:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-04
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 2014-06-01
  • 2018-10-05
  • 2012-08-03
相关资源
最近更新 更多