我是React的中级开发人员,我想就最佳实践提出建议,以在reducer中使用SET_STATE更新道具。
这是我的文件名称:
redux/
├── events/
│ ├── reducer.js
│ ├── actions.js
│ ├── sagas.js
├── services/
│ ├── events.js
[当我在sagas.js中调度动作时,该动作函数会在events.js中调用另一个调用API的函数。我想在每次修改之后更新我的网页的props top revent reload。
现在,当我在sagas.js中用yield call(...)在events.js中调用一个函数时,在返回API响应数据时,我总是收到undifined。
我希望收到此信息以在我的减速器中调用我的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)。
您有什么想法吗?非常感谢您的宝贵帮助
我怀疑这是您的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,
})
})
}