redux - Redux:为主减速器的子元素构建减速器
问题描述
可能是个愚蠢的问题。我有一个从 Immutable.js 记录构建的 Redux reducer:
export const Editors = new Record({
'editors': new List(), // this is a list of Editor items below
})
export const Editor = new Record({
'id': null,
'state': EditorState.createEmpty(),
'annotations': new List(),
});
我以这种方式组织事物,因为我希望我正在构建的应用程序有很多编辑器。
但是,我想编写一个updateState
更新单个编辑器状态的操作。Editor
我想如果我能够编写对特定而不是Editor
在我的减速器中的 s列表起作用的减速器代码,那么编写它会容易得多Editors
。有没有办法编写 reducer 代码,这样我就可以在单个编辑器中更新字段的状态,而不是在列表中找到编辑器、删除它、更新它、重新插入它等等?
解决方案
export const Editors = new Record({
'editors': new Map(), // this is a list of Editor items below
})
export const Editor = new Record({
'id': null,
'state': EditorState.createEmpty(),
'annotations': new List(),
});
然后更新编辑器变为:
const updateEditorState = (Editors, id, editorState) =>
Editors.setIn(['editors', id, 'state']¸editorState)
顺便说一句,你的命名有点奇怪。我会小写Editors
和Editor
.
推荐阅读
- python-3.x - 使用字符串连接参数化的 Sparql 查询
- regex - 如何使用单个字符替换多个数字
- django - Django 表 - 行中的数据
- jsf - Java JSF 和 Bean 连接的问题
- powershell - How to pass powershell parameters into Chef
- reactjs - componentDidMount 没有渲染所有内容
- javascript - 在 React 中测试 Paypal 按钮(茉莉和酶)
- scala - 如何从 Scala 中的特征访问类型成员
- python - 在类 (OOP) 中实现 tkinter Frame 时遇到问题 [AttributeError: type object 'Frame' has no attribute 'tk']
- c# - 方法返回 'System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]' 而不是结果