reactjs - React/Redux - 编辑列表项后列表不更新
问题描述
我有一个显示在表格中的对象列表。编辑列表中的项目时,除非我刷新页面,否则不会显示更新的项目。
列表减速器
export const eaCodesReducer = (state = initialState.eaCodes, action) => {
switch (action.type) {
case "GET_EA_CODES": {
return {
...state,
eaCodes: action.payload
}
}
default: {
return state
}
}
}
单品减速机
export const eaSingleEaCodeReducer = (state = initialState.eaCode, action) => {
switch (action.type) {
case "UPDATE_EA_CODE": {
return {
...state,
eaCode: action.payload
}
}
default:
return state
}
}
然后我如何告诉它更新 eaCodes 列表以合并单个项目更改?
解决方案
@D10S 有正确的想法,但导致应用程序崩溃。而不是添加额外的减速器 - 如果初始放置请求成功,我更新了调用减速器的操作:
export const updateEnforcementActionCode = (id, updateTypeObj) => {
return dispatch => {
return axios.put(`/api/enforcementactions/codes/${id}`, updateTypeObj)
.then(res => {
dispatch({ type: "UPDATE_EA_CODE", payload: res.data })
if (res.status === 200) {
return axios.get('/api/enforcementactions/codes')
.then(res => {
dispatch({ type: "GET_EA_CODES", payload: res.data })
})
}
}).catch(err => {
dispatch({ type: "GET_ERRORS", payload: err.response.message })
})
}
}
这似乎有点多余。如果有人建议让这个更光滑,我很开放。
推荐阅读
- android - Glide 可以在 Android TextView 中渲染 ImageSpan 吗?
- python - 如何在 embed mod 中更改 Ipython 输入/输出提示
- python - 根据标题python的名称和数量订购原始数据
- snowflake-cloud-data-platform - 我们正在尝试将 ACCOUNTADMIN 角色分配给用户,但由于某种原因,它似乎没有生效或更新..不知道为什么?
- .net - 为什么 Visual Studio NugetPackageManager 中的“版本”列对于 .NET Framework 项目显示为空白?
- node.js - 使用 Passport、Fastify 的 GoogleStrategy 中的 invalid_grant 问题
- python-3.x - 将标头与文件数据分开发送HTTP PUT python urllib
- php - 为什么 EventSubscriberInterface 不起作用如果我想获得一个 html 页面
- apache-kafka - Kafka Streams 标点时间戳在上下文时间戳之前
- angular - Angular 9:如何从 templateRef 访问子组件实例