javascript - 不可变的 JS 和 redux
问题描述
我正在redux
使用Immutable JS
. Mystate
是一个Immutable JS
对象,类似于
const initialState = Immutable.fromJS({
bar: 1
foo: {
bar: 'a',
foo: 'b',
foobar: [
{
...
...
...
现在我有了减速器,通常我会这样做
export function foo(state = initialState, action) {
switch (action.type) {
case FOO:
return {
...state,
foo: {
...state.foo,
bar: action.value
}
};
...
但是,现在我正在使用Immutable JS
我需要做的
export function foo(state = initialState, action) {
switch (action.type) {
case FOO:
return {
...state, // do I need to change something here too?
foo: {
...state.get('foo'), // this changed
bar: action.value
}
};
...
但是,这与我的商店混乱。我失去了一切,但是bar: action.value
。好像...state.get('foo')
不等于...state.foo
。
我该如何正确地做到这一点?还是我在这里用Immutable JS
错了?
解决方案
你应该能够做到:
return state.set(['foo', 'bar'], value)
数组是对象嵌套属性中的路径,最后一个是新值。
推荐阅读
- python - CTYPES 使用 python3.8 编写访问冲突,而 2.7 工作
- blazor - Blazor - 使用服务在组件之间共享数据
- html - 图像在 div 中垂直对齐
- java - 了解继承在 Java 中的工作原理
- python - 如何阻止 Python 识别字符串文字,例如“\n”或“\b”?
- ios - NSInputStream 可以用来读取带有偏移量的数据块,以便能够上传大文件
- ionic-framework - 如何证明 ion-item 中的内容是合理的
- javascript - 声明变量并使用 getElementById 打印 Hello World
- angular - 如何覆盖角度形式控制的测试?(附上Stackblitz)
- java - 如何控制带有if条件的for循环在循环完成之前不输出