Rule

The library expose some functions:

import { Rule } from 'redux-autoreducers'

constructor

Description: Instance constructor of Rule.

Params:

Param Type Required
type String True

Example:

const rule = new Rule('GET_ALL');

overrideReducer

Description: Instance method that allow to override reducer or reducer generator.

Params:

Param Type Required
overrideGenerateReducer Object or Function (take same params as defaults/overrideGenerateReducer) True

overrideGenerateReducer function must return an object with https://github.com/tomatau/type-to-reducer schema.

overrideGenerateReducer object must be a https://github.com/tomatau/type-to-reducer schema.

Function has same params has describe in Defaults overrideGenerateReducer.

Example with object:

const rule = new Rule('OPEN');
rule.overrideReducer({
    OPEN: state => ({
        ...state,
        isOpen: true,
    }),
});

Example with function:

const rule = new Rule('OPEN');
rule.overrideReducer((type, lowerCamelCaseType, typeSuffixes) => ({
    [type]: {
        [typeSuffixes.PENDING]: state => ({
            ...state,
            [`${lowerCamelCaseType}Data`]: null,
            [`${lowerCamelCaseType}Error`]: null,
        }),
        [typeSuffixes.FULFILLED]: (state, action) => ({
            ...state,
            [`${lowerCamelCaseType}Data`]: action.data,
            [`${lowerCamelCaseType}Error`]: null,
        }),
        [typeSuffixes.REJECTED]: (state, action) => ({
            ...state,
            [`${lowerCamelCaseType}Data`]: null,
            [`${lowerCamelCaseType}Error`]: action.error,
        }),
    },
}));

overrideInitialState

Description: Instance method that allow to override initial state or initial state generator.

Params:

Param Type Required
overrideGenerateInitialState Object or Function (take same params as defaults/overrideGenerateInitialState) True

Example with object:

const rule = new Rule('OPEN');
rule.overrideInitialState({
    isOpen: true,
});

Example with function:

const rule = new Rule('OPEN');
rule.overrideInitialState((lowerCamelCaseType) => ({
    [`${lowerCamelCaseType}Data`]: null,
    [`${lowerCamelCaseType}Error`]: null,
}));

overrideActionTransformer

Description: Instance method that allow to override action transformer.

Params:

Param Type Required
actionTransformer Function True

Example:

const rule = new Rule('OPEN');
rule.overrideActionTransformer((action) => {
    if (!action.payload) {
        return action;
    }

    const { type, payload: { error, data } } = action;
    return {
        type,
        error,
        data,
    };
})

results matching ""

    No results matching ""