gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:50:35 24 4
gpt4 key购买 nike

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

这是我的文件命名法:

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

当我在 sagas.js 中分派(dispatch)一个 Action 时, Action 函数会调用 events.js 中调用 API 的另一个函数。我想在每次修改后更新 props top revent 重新加载我的网页。

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

我想收到此信息以在我的 reducer 中调用我的 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.jsyield 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,
})
})
}

关于javascript - Redux - 返回调用函数信息以更新 Prop /状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58336450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com