0.1.1 • Published 9 years ago
@beardedtim/create-action-reducer v0.1.1
Create Action Reducer
Abstractions suck so let's build an abstraction
Usage
import {createAction, createReducer} from '@beardedtim/create-action-reducer'
const ADD_USER = `ADD_USER`
const addUser = createAction(ADD_USER)
const userActions = {
[ADD_USER]: (state,user) => [...state,user]
}
const userReducer = createReducer(userActions)Now addUser is a normal redux action creator and userReducer is a normal redux reducer. addUser is waiting for payload and userReducer is waiting for (state,action)
createAction
This will create an action of {type,payload} using the following api:
const action = createAction(ACTION_TYPE)
action(payload) // ({type: ACTION_TYPE, payload: payload})- This returns a function that is your 'action creator' and the returned function is your 'action'
createReducer
This will create a reducer with a default key that just returns the state. This needs your actions to be given as a dictionary/hash/object like so:
const actions = {
[ADD_USER]: (state,payload) => [...state,payload]
}and it can be used like so:
const actions = {
...
}
const reducer = createReducer(actions)
reducer(state,action) // new state