1.0.5 • Published 7 years ago

redux-rac-utils v1.0.5

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

redux-rac-utils

Build Status codecov

This is a set of utils for creating redux actions creators and reducers.

Getting started

npm install --save redux-rac-utils

Creating a reducer

This helper creates a redux reducer. It relies on the fact that an action object must be FSA compliant.

import { reducerFactory } from 'redux-rac-utils';

const initialState = {
  value: 0,
};

const reducer = reducerFactory(
  initialState,
  {
    INC: (state, action) => state + action.payload,
    DEC: (state, action) => state - action.payload,
  }
);

reducer();

/*
{
  value: 0
}
*/

Creating an actions creator

import { actionsCreatorFactory } from 'redux-rac-utils';

const actionsCreator = actionsCreatorFactory(
  'INC'
);

actionsCreator();

/*
{
  type: 'INC'
}
*/

actionsCreator(5);

/*
{
  type: 'INC',
  payload: 5
}
*/

You could also provide payloadCreator and metaCreator (similar to redux-actions).

import { actionsCreatorFactory } from 'retax';

const actionsCreator = actionsCreatorFactory(
  'INC',
  x => 2 * x,
  y => 3 * y
);

actionsCreator();

/*
{
  type: 'INC'
}
*/

actionsCreator(5);

/*
{
  type: 'INC',
  payload: 10,
  meta: 15
}
*/