Skip to content

Latest commit

 

History

History
51 lines (43 loc) · 1.84 KB

reducer-modification.md

File metadata and controls

51 lines (43 loc) · 1.84 KB

Modifying Reducers

In your entry script the reducer transformer must be added to the injector.

import Injector from 'lib/Injector';
import ReducerTransformer from '../reducers/ReducerTransformer';

Injector.transform('my-transform', (updater) => {
  updater.reducer('locator', ReducerTransformer);
});

from react-locator-test client/src/js/boot/index.js

The reducer transformer can now be created like any other react injector reducer transformer.

const MyReducerTransformer = (originalReducer) => (globalState) => (state, { type, payload }) => {
    switch (type) {
        case 'MARKER_CLICK': {
            return {
                ...originalReducer(state, { type, payload }),
                markerOpen: true,
            };
        }

        case 'MARKER_CLOSE': {
            return {
                ...originalReducer(state, { type, payload }),
                markerOpen: false,
            };
        }

        default: {
            if (state === undefined) {
                return originalReducer(state, {type, payload});
            }
            // keeps markerOpen in the state. Disappears if another action is run
            return {
                ...originalReducer(state, {type, payload}),
                markerOpen: state.markerOpen,
            };
        }
    }
};

export default MyReducerTransformer;

from react-locator-test client/src/js/reducers/ReducerTransformer.js

All reducer transformers require all modifications to return originalReducer(state, { type, payload }) with or without modified data. If it does not return with the originalReducer the locator may not work properly.