create-redux-reducer

1.1.0 • Public • Published

create-redux-reducer

Installation

npm install create-redux-reducer --save-dev

Description

Creates reducer actions object where you can describe actions as computed keys. Receives 2 parameters: the initial state of reducer and object with callbacks for each type of action. Each type callback receives 3 parameters: current state, payload and error. In your action creators you must always return an object with type and payload keys to follow the actions convention.

Usage

import createReducer from 'create-redux-reducer';
...

// reducers/auth.js
const initialState = {
    isAuthorized: false,
    isLoading: false,
    error: false,
    user: {}
};

export default createReducer(initialState, {
    [LOGIN_REQUEST]: (state, payload) => ({
        ...state, isLoading: true
    }),
    [LOGIN_SUCCESS]: (state, payload) => ({
        ...state, isLoading: false, isAuthorized: true, user: payload, error: false
    }),
    [LOGIN_FAILURE]: (state, payload) => ({
        ...state, error: payload, user: {}, isAuthorized: false, isLoading: false
    })
});

Package Sidebar

Install

npm i create-redux-reducer

Weekly Downloads

4

Version

1.1.0

License

ISC

Last publish

Collaborators

  • malyusha