Basics

Rule

Rule is a class that will create reducer (based on https://github.com/tomatau/type-to-reducer format) and initial state for reducer function.

Rule is the base of all the generator. This class take the super event type and will create reducer to manage all subevents.

For example, the Rule will be created like that:

import { Rule } from 'redux-autoreducers';

const getAllRule = new Rule('GET_ALL');

This will manage events in reducer:

  • GET_ALL_PENDING
  • GET_ALL_FULFILLED
  • GET_ALL_REJECTED

This will also create initial state:

{
    isGetAllPending: false,
    getAllData: null,
    getAllError: null,
}

Generator

Generator class will aggregate all Rules to create a reducer with rules reducers and rules initial states.

Example:

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

const getAllRule = new Rule('GET_ALL');
const generator = new Generator([getAllRule]);

const reducer = generator.generate();

Actions

Actions must have this schema to be correctly managed:

{
    "type": { type: String, required: true },
    "data": { type: Any },
    "error": { type: Any }
}

The strategy is (with 'GET_ALL' example):

  • When 'PENDING' event :
    • Put isGetAllPending to true
  • When 'FULFILLED' event:
    • Put isGetAllPending to false
    • Put getAllData to action.data
  • When 'FULFILLED' event:

    • Put isGetAllPending to false

    • Put getAllData to action.error

results matching ""

    No results matching ""