1.6.0 • Published 5 years ago

redux-compose-reducer v1.6.0

Weekly downloads
1,666
License
MIT
Repository
github
Last release
5 years ago

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).

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago