Immutable.js should be used in each reducer as necessary, for example
import { ACTIVATE_LOCATION } from './actions'; import Immutable from 'immutable'; export let ui = (state, action) => { switch (action.type) { case ACTIVATE_LOCATION: state = Immutable .fromJS(state) .set('activeLocationId', action.id) .toJS(); break; } return state; };
However, this example has a lot of overhead: every time a reducer action is called, it must convert the JavaScript object to an Immutable instance, mutate the resulting object, and convert it back to a JavaScript object.
A better approach is to have an initial state of an Immutable instance:
import { ACTIVATE_LOCATION } from './actions'; import Immutable from 'immutable'; let initialState = Immutable.Map([]); export let ui = (state = initialState, action) => { switch (action.type) { case ACTIVATE_LOCATION: state = state.set('activeLocationId', action.id); break; } return state; };
Thus, you only need to convert the initial state to an instance of Immutable ounce. Then each reducer will consider it as an Immutable instance and pass it line by line as an Immutable instance. The trick is that now you need to pass all the JavaScript state before passing the values ββto the view context.
If you perform multiple state mutations in a reducer, you may need batch mutations using .withMutations .
To simplify the task, I developed the redux-immutable library. It provides a function combineReducers , equivalent to a function of the same name in the redux package, except that it expects the initial state and all reducers to work unchanged. js .
Gajus Aug 08 '15 at 11:44 2015-08-08 11:44
source share