Wie verwende ich Immutable.js mit Redux?

Redux Rahmen ist mit Reduzierstücke zu ändern, app-Zustand in Reaktion auf eine Aktion.

Die entscheidende Voraussetzung ist, dass ein Druckminderer nicht mehr ändern eines vorhandenen Status-Objekt; es darf produzieren einem neuen Objekt.

Schlechtes Beispiel:

import {
    ACTIVATE_LOCATION
} from './actions';

export let ui = (state = [], action) => {
    switch (action.type) {
        case ACTIVATE_LOCATION:
            state.activeLocationId = action.id;
            break;
    }

    return state;
};

Gutes Beispiel:

import {
    ACTIVATE_LOCATION
} from './actions';

export let ui = (state = [], action) => {
    switch (action.type) {
        case ACTIVATE_LOCATION:
            state = Object.assign({}, state, {
                activeLocationId: action.id
            });
            break;
    }

    return state;
};

Dies ist ein guter use-case für Immutable.js.

InformationsquelleAutor der Frage Gajus | 2015-08-08

Schreibe einen Kommentar