Skip to content

kimrgrey/redux-compose-reducer

Repository files navigation

Redux Compose Reducer build status npm version npm downloads license tested with jest

Reducer's composer for Redux. This library simplifies workflow for building actions (general and namespaced) and reducers for them in Redux.

Installation

Install from the NPM repository using yarn or npm:

yarn add redux-compose-reducer
npm install --save redux-compose-reducer

Usage

To create types for your actions you can use function createTypes in this way:

import { createTypes } from 'redux-compose-reducer'

export const TYPES = createTypes(['openSideBar', 'closeSideBar'])

export const openSideBar = () => ({
  type: TYPES.openSideBar,
})

export const closeSideBar = () => ({
  type: TYPES.closeSideBar,
})

After it you need to create reducers for this types, right? Try this:

import { composeReducer } from 'redux-compose-reducer'
import { TYPES } from './actions'

const openSideBar = state => ({
  ...state,
  open: true,
})

// if your reducer doesn't require state and action
// you can declare it as object that describes changes
const closeSideBar = { open: false }

export default composeReducer({
  types: TYPES,
  initialState: { open: false },
  reducers: {
    openSideBar,
    closeSideBar,
  },
})

Usage with namespaces

To create namespaced types you need to update createTypes function call:

import { createTypes } from 'redux-compose-reducer'

export const TYPES = createTypes('app', ['openSideBar', 'closeSideBar'])

In such case your TYPES object will be:

{
  openSideBar: 'app/openSideBar',
  closeSideBar: 'app/closeSideBar',
}

Your reducer's code remains the same (see previous example).

About

Reducer's composer for Redux

Resources

Stars

Watchers

Forks

Packages

No packages published