1.适当封装 Redux,简化调用

src/utils/redux.js

/**
 * 适当封装 Redux,简化调用
 */
/* eslint-disable import/prefer-default-export */
import fetch from './request'

export function createAction(options) {
  const { url, payload, method, fetchOptions, cb, type } = options
  return (dispatch) => {
    return fetch({ url, payload, method, ...fetchOptions }).then((res) => {
      dispatch({ type, payload: cb ? cb(res) : res })
      return res
    })
  }
}

2.调用

src/actions/user.js

import { API_USER_LOGIN } from '@constants/api'
import { createAction } from '@utils/redux'

/**
 * 用户登录
 * @param {*} payload
 */
export const dispatchLogin = payload => {
  return createAction({
    url: API_USER_LOGIN, // api请求地址
    type: USER_LOGIN,
    payload
  })
}

.

相关文章:

  • 2022-03-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2021-07-15
  • 2021-06-16
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-10
相关资源
相似解决方案