javascript - 我是redux的新手。我一次只能访问一个键值,另一个不工作
问题描述
提交时只获取名称或任务,如何同时接收两个键值对
import * as actionTypes from './actions'
const initialState = {
formData: {
name: "",
task: ""
}
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'SAVE_FORM_DATA':
return {
...state,
formData: {
formData: action.payload
}
}
default:
return state;
}
}
export default reducer;
解决方案
您可以通过 2 种方法做到这一点:
1)您可以克隆您的状态并在克隆的对象中进行必要的修改并返回它,如我在示例中所示
2)或者,您可以探索“更新反应插件”,用于更新所需的参数,同时保持状态不可变
import * as actionTypes from './actions'
const initialState = {
formData: {
name: "",
task: ""
}
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'SAVE_FORM_DATA':
let jasper = Object.assign({}, state);
return {
jasper:action.payload
}
}
default:
return state;
}
}
export default reducer;
推荐阅读
- python - 尝试使用 python 和 bs4 从特定 'td' 中抓取所有 'a' 文本
- android - 如何在 API 级别 15 (Android 4.0.3) 上创建 Android (AVD) 模拟器
- python - 在另一个列表中查找包含 5 个连续字符串的单词
- python - 如何使用熊猫系列对条件进行矢量化?
- python - 在 Django 中测试 Url 是否给出 500 错误
- c# - 关闭的表单仍显示在任务栏中
- c# - 请参阅 Unity Inspector 中数组的枚举索引
- c++ - 为什么我的编译器会显示有关我的 constexpr 函数的此错误?
- java - 如何从远程源 (url) 获取图像并在 ImageView 中显示?
- sql-server - 在同一记录中重新插入主键