reactjs - 为什么我的 reducer 将新对象添加到我的 redux 存储而不是更改当前对象值?
问题描述
我在使用 React-Redux 时遇到问题,我想更新商店中的特定值,但我在商店中获得了新对象。
我的动作类型:
export const SENDMESSAGE = "SENDMESSAGE";
我的动作创建者:
export const updateContactFormData = (payload) => ({
type: SENDMESSAGE,
payload: payload
})
我的初始状态:
export const formInitialData = {
formShape: {...},
formData: {...},
formContactData: {
name: '',
email: '',
text: ''
}
}
我的减速机看起来像这样:
export default function formReducer(storeData, action) {
switch(action.type) {
case SENDMESSAGE:
return {
...storeData,
[storeData.formContactData]: action.payload,
}
default:
return storeData || formInitialData
}
}
现在我在我的商店中获得了新对象,正如 Redux 工具所示: 在此处输入图像描述
解决方案
您想要更新属性值,而不是使用以前的值作为名称来定义新属性。
return {
...storeData,
formContactData: {...storeData.formContactData, ...action.payload},
}
推荐阅读
- sql - 在 BigQuery/Standard SQL 中使用数组确定给定日期的状态
- swift - 如何解决 UIResponder 无法识别的问题?
- user-interface - 有人可以解释如何添加滑块对象以控制自己的伺服电机吗?
- python - Plotly-Dash:3 个回调,相同的输入列表 - 2 个被调用,1 个不被调用
- login - Identity Server 4 注册用户后自动登录
- java - Firebase MLKit 可以用来检测多边形吗?
- c++ - 通过 C++ 单击按钮后更新 QML TableView
- visual-studio-code - 由于“代码 - Insiders.exe”上的访问被拒绝错误,安装和更新失败
- python - 遇到错误TypeError: can only concatenate tuple (not "list") to tuple
- r - 如何根据其他行计算百分比