1.0.3 • Published 9 years ago

@micro-js/create-action v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
9 years ago

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

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago