Redux-返回调用函数信息以更新道具/状态

时间:2019-10-11 07:40:53

标签: javascript reactjs redux redux-saga

我是React的中级开发人员,我想就在reducer中使用SET_STATE更新道具的最佳实践提供建议。

这是我的文件名称:

redux/
├── events/
│   ├── reducer.js
│   ├── actions.js
│   ├── sagas.js
├── services/
│   ├── events.js

当我在sagas.js中调度动作时,该动作函数会在events.js中调用另一个调用API的函数。 我想在每次修改后更新网页的props top revent reload。

现在,当我在events.js中使用yield call(...)[在sagas.js中]调用函数时,我总是收到 未定义 ,同时我返回API响应数据。

我想收到此信息以调用我的化简器中的 SET_STATE 操作以更新它而无需重新声明

这是我的文件:

reducer.js

import actions from './actions'

const initialState = {
  uniqueEvent: {},
  logistic: []
}

export default function eventsReducer(state = initialState, action) {
  switch (action.type) {
    case actions.SET_STATE:
    {
      return { ...state, ...action.payload }
    }
    default:
      return state
  }
}

actions.js

const actions = {
  SET_STATE: 'events/SET_STATE',
  UPDATE_EVENT: 'events/UPDATE_EVENT',
  ADD_EVENT_LOGISTIC: 'events/ADD_EVENT_LOGISTIC',
}

export default actions

sagas.js

import { all, takeEvery, put, call } from 'redux-saga/effects'
import { pdateEvent, addEventLogistic } from 'services/events'
import actions from './actions'


export function* UPDATE_EVENT({ payload: { event } }) {
  console.log(JSON.stringify(`UPDATE_EVENT`))

  const upEvent = yield call(updateEvent, event)
  if(upEvent){
    yield put({
        type: 'events/SET_STATE',
        payload: {
          uniqueEvent: upEvent,
        },
      })
  } else {
     console.log(JSON.stringify('REDUX UPDATE_EVENT NOT DONE'))
   }
}

export function* ADD_EVENT_LOGISTIC({ payload: { activist, information, eventId } }) {
  console.log(JSON.stringify(`ADD_EVENT_LOGISTIC`))

  const logisticToAdd = {user: activist, description: information, event: eventId}

  const eventLogistic = yield call(addEventLogistic, logisticToAdd)
  if (eventLogistic) {
    yield put({
        type: 'events/SET_STATE',
        payload: {
          logistic: eventLogistic,
        },
      })
  } else {
    console.log(JSON.stringify('REDUX EVENT LOGISTIC NOT DONE'))
  }
}

export default function* rootSaga() {
  yield all([
    takeEvery(actions.UPDATE_EVENT, UPDATE_EVENT),
    takeEvery(actions.ADD_EVENT_LOGISTIC,ADD_EVENT_LOGISTIC),
  ])
}

还有我的events.js(API调用):

import { notification } from 'antd'

const axios = require('axios')

export function updateEvent(value) {
  axios.put(`http://api.xxxx.xx/xx/xxxx/events/event/${value.id}/`, value, {
    headers: {
      Authorization: `Token ${localStorage.getItem('auth_token')}`,
    },
  })
    .then(response => {
      return response.data
    })
    .catch(function(error) {
      notification.error({
        message: error.code,
        description: error.message,
      })
    })
}

export function addEventLogistic(value) {
  axios.post(`http://api.xxxx.xx/xx/xxxx/events/logistic/`, value, {
    headers: {
      Authorization: `Token ${localStorage.getItem('auth_token')}`,
    },
  })
    .then(response => {
      return response.data
    })
    .catch(function(error) {
      notification.error({
         message: error.code,
         description: error.message,
      })
    })
}

export default async function defaulFunction() {
  return true
}

我希望 events.js 中的addEventLogstic()函数从请求中返回JSON(我做了一些测试并且都很好),但是在 sagas.js 用 yield call(...)声明的变量没有任何值(而我想要API的JSON)。

您有什么想法吗? 非常感谢您的宝贵帮助

1 个答案:

答案 0 :(得分:2)

我怀疑这是您的updateEvent和addEventLogistic函数未返回任何内容。添加一个return语句,以便他们这样做:

export function updateEvent(value) {
  //added return statement here
  return axios.put(`http://api.xxxx.xx/xx/xxxx/events/event/${value.id}/`, value, {
    headers: {
      Authorization: `Token ${localStorage.getItem('auth_token')}`,
    },
  })
    .then(response => {
      return response.data
    })
    .catch(function(error) {
      notification.error({
        message: error.code,
        description: error.message,
      })
    })
}