0.0.2 • Published 6 years ago

convenient-redux-helpers v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

convenient-redux-helpers

Introduction

This simple library provides some function factory to build Redux components with a single function call. See the testing documentation for usage.

Getting Started

Install with NPM or Yarn

yarn add convenient-redux-helpers

Import the factory for the component and default flags.

import {
  reducerFactory,
  middlewareFactory,
  actionFactory,
  DEFAULT_HANDLER,
  DEFAULT_STATE
} from 'convenient-redux-helpers'

const INCREMENT_BY = "NAMESPACE_INCREMENT_BY"

const triggerDefaultHandler = actionFactory()

const incrementBy = actionFactory(INCREMENT_BY)

const counterReducer = reducerFactory({

  [DEFAULT_STATE]: { value: 0 },

  [DEFAULT_HANDLER]: (state, action) => {
    Object.assign({}, state, {
      value: state.value + 1
    })
  },

  [INCREMENT_BY]: (state, action) => {
    Object.assign({}, state, {
      value: state.value + action.payload
    })
  }
})

const counterMiddleware = middlewareFactory({

  [DEFAULT_HANDLER]: (store, next, action) => {
    const originalValue = store.getState().counter.value
    const newState = next(action)
    const newValue = newState.counter.value
    console.log(`Counter increment from ${originalValue} to ${newValue}.`)
    return newState
  }

})