@micro-js/create-action

1.0.3 • Public • Published

create-action

Build status Git tag NPM version Code style

Action creator creator for flux standard actions. Very similar to the function of the same name found in redux-actions, but implemented as a micro-module and without the special error handling stuff.

Installation

$ npm install @micro-js/create-action

Usage

var createAction = require('@micro-js/create-action')
var incrementBy = createAction(INCREMENT_BY)

// ...

dispatch(incrementBy(2))

API

createAction(type, payloadCreator, metaCreator)

  • type - String or other identifier that represents the type of the action to create
  • payloadCreator - Function that produces payload from the arguments to the action creator (optional - defaults to https://github.com/micro-js/identity)
  • metaCreator - Function that produces meta from the arguments to the action creator (optional)

Returns: An action creator that creates an action according to the arguments specified.

toString sugar

Action creators returned by createAction override Function.prototype.toString, and instead return the type string. This allows the function to double as the action type, for useThis means that instead of writing this:

const SET_TEXT = 'SET_TEXT'
const setText = createAction('SET_TEXT')

export {
  setText,
  SET_TEXT
}

You can just write this:

export default createAction('SET_TEXT')

And then use it in a reducer map, like this:

combineReducers({
  text: handleActions({
    [setText]: (state, text) => text
  })
})

type property

If abusing toString as above makes you squeamish, then the type is also set on the type property of the action creator, so that you can access it that way as well, if you prefer.

License

MIT

/@micro-js/create-action/

    Package Sidebar

    Install

    npm i @micro-js/create-action

    Weekly Downloads

    0

    Version

    1.0.3

    License

    MIT

    Last publish

    Collaborators

    • micro-js