How to use Immutable.js with redux?

后端 未结 5 849
-上瘾入骨i
-上瘾入骨i 2020-12-07 09:53

Redux framework is using reducers to change app state in response to an action.

The key requirement is that a reducer cannot modify an existing state object; it must

5条回答
  •  被撕碎了的回忆
    2020-12-07 10:14

    If you're just looking for an easy way to make updates without mutation, I maintain a library: https://github.com/substantial/updeep which, in my opinion, is a good way to do that with redux.

    updeep lets you work with regular (frozen) object hierarchies, so you can do destructuring, see objects in logs and the debugger, etc. It also has a powerful API that allows for batch updating. It's not going to be as efficient as Immutable.js for large data sets because it does clone objects if it needs to.

    Here's an example (but check those in the README for more):

    import {
        ACTIVATE_LOCATION
    } from './actions';
    import u from 'updeep';
    
    export let ui = (state = [], action) => {
        switch (action.type) {
            case ACTIVATE_LOCATION:
                state = u({ activeLocation: action.id }, state);
                break;
        }
    
        return state;
    };
    

提交回复
热议问题