Aim of the library

The aim is to avoid creating and maintaining "same" reducer code. This can be boring and take a lot of time.

This library will generate reducer automatically based on a convention.

This will allow you to transform:

// Initial State
const initialState = {
    isTestPending: false,
    testData: null,
    testError: null,
    isGetPending: false,
    getData: null,
    getError: null,
    isOpen: false,
};

// Exports
export default reducer;

// Define reducer
function reducer(state = initialState, action) {
    switch (action.type) {
        case 'TEST_PENDING':
            return {
                ...state,
                isTestPending: true,
                testData: null,
                testError: null,
            };
        case 'TEST_REJECTED':
            return {
                ...state,
                isTestPending: false,
                testError: action.error,
            };
        case 'TEST_FULFILLED':
            return {
                ...state,
                isTestPending: false,
                testData: action.data,
            };
        case 'GET_PENDING':
            return {
                ...state,
                isGetPending: true,
                getData: null,
                getError: null,
            };
        case 'GET_REJECTED':
            return {
                ...state,
                isGetPending: false,
                getError: action.error,
            };
        case 'GET_FULFILLED':
            return {
                ...state,
                isGetPending: false,
                getData: action.data,
            };
        case 'OPEN':
            return {
                ...state,
                isOpen: true,
            };
        case 'CLOSE':
            return {
                ...state,
                isOpen: false,
            };
        default:
            return state;
    }
}`

In:

// Imports
import { Rule, Generator } from 'redux-autoreducers';

///////////
// Rules //
///////////
// Create a simple rule for 'TEST' actions
const testRule = new Rule('TEST');
// Create a simple rule for 'GET' actions
const getRule = new Rule('GET');
// Create a rule for 'OPEN' actions
const openRule = new Rule('OPEN');
openRule
    .overrideInitialState(() => ({ isOpen: false }))
    .overrideReducer(() => ({
        OPEN: state => ({
            ...state,
            isOpen: true,
        }),
    }));
// Create a rule for 'CLOSE' actions
const closeRule = new Rule('CLOSE');
closeRule
    .overrideInitialState(() => ({}))
    .overrideReducer(() => ({
        CLOSE: state => ({
            ...state,
            isOpen: false,
        }),
    }));

///////////////
// Generator //
///////////////
const generator = new Generator([testRule, getRule, openRule, closeRule]);
const reducer = generator.generate();

// Exports
export default reducer;

With this generator, reducers have less code, it is easier to update and to understand.

results matching ""

    No results matching ""