@micro-js/create-action
v1.0.3
Published
Action creator creator for flux standard actions
Downloads
1
Readme
create-action
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 createpayloadCreator
- Function that producespayload
from the arguments to the action creator (optional - defaults to https://github.com/micro-js/identity)metaCreator
- Function that producesmeta
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