简体   繁体   English

不可变的JS和Redux

[英]Immutable JS and redux

I am using redux with Immutable JS . 我正在将reduxImmutable JS My state is a Immutable JS object, something like 我的state是一个Immutable JS对象,类似

const initialState = Immutable.fromJS({
    bar: 1
    foo: {
        bar: 'a',
        foo: 'b',
        foobar: [
            {
               ...
        ...
    ...

Now I have the reducer and normally I would do 现在我有了减速器,通常我会做

export function foo(state = initialState, action) {
    switch (action.type) {
        case FOO:
            return {
                ...state,
                foo: {
                    ...state.foo,
                    bar: action.value
                }
            };
        ...

But, now that I am using Immutable JS I need to do 但是,现在我正在使用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
                }
            };
        ...

However, that messes with my store. 但是,这使我的商店陷入混乱。 I loose everythng but bar: action.value . 除了bar: action.value我什么都松了。 It seems like ...state.get('foo') does not equal to ...state.foo . 似乎...state.get('foo')不等于...state.foo

How do I do it properly? 如何正确执行? Or am I using Immutable JS wrong here? 还是我在这里使用Immutable JS错误?

You should be able to do just: 您应该能够做到:

return state.set(['foo', 'bar'], value)

The array is the path in you object nested properties, the last one is the new value. 数组是对象嵌套属性中的路径,最后一个是新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM