redux-actions-class

1.0.4 • Public • Published

redux-actions-class

Build Status

Create action creators and types with minimal redundancy.

Install

npm i redux-actions-class --save

Why not redux-actions or redux-act?

The benefits of this package compared to redux-actions is that you don't have to manage a separate actionTypes file; you get to define actions on an orderly object literal and all your types and creators will live on just one object. Just a matter of preference.

Considering redux-act, we achieve a similar goal of not having to declare or manage string constants, however, this package still enforces the SCREAMING_UNDERSCORE_CASING we are so used to.

In any case, both of those package offer some goodies beyond actions, like higher level reducer creators. For that I recommend coupling this package with redux-create-reducer - from the example in the redux reducing boilerplate section. As far as FSA enforcement goes, I got nothing but am open to PRs and suggestions.

Example

import Actions from 'redux-actions-class'
import { assert } from 'chai'
 
const actions = new Actions({
  
  // use falsy value for actions with no payloads
  DISPLAY_COLORS: null,
  
  // use string for single argument (like payload, if you use FSAs)
  ADD_COLOR: 'color',
  
  // use array for multiple arguments
  MIX_COLORS: ['color1', 'color2']
  
  // thunks work, too
  ADD_COLORS (...colors) {
    return dispatch => colors.forEach(color => dispatch(this.addColor(color)))
  }
})
 
// all keys from above spec will be added directly to your actions object
actions.DISPLAY_COLORS
// => 'DISPLAY_COLORS'
 
// all creators in camelCase form that can be dispatched are added too
typeof actions.displayColors
// => 'function'
 
// all types are also grouped together on the `types` field
// (the values are identical)
actions.types.DISPLAY_COLORS
// => 'DISPLAY_COLORS'
 
// likewise, all action creators are available through the creators key
import { bindActionCreators } from 'redux'
 
const mapDispatchToProps = dispatch => {
  bindActionCreators(actions.creators, dispatch)
}

License

MIT

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.4
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.4
    0
  • 1.0.3
    0
  • 1.0.2
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i redux-actions-class

Weekly Downloads

0

Version

1.0.4

License

MIT

Last publish

Collaborators

  • lokua