ricardo-simple-redux
Reduce amount of boilerplate when building simple actions/reducers
Why SimpleRedux?
Current structure actions + reducers is overcomplex and requires too much bolerplate 😉 .
Why is it better than what we have today?
- Less code === less bugs
- Less code === less time needed to code (more time for table soccer)
- We can (almost) get rid of action names
- More testable
Installation
yarn add ricardo-simple-redux
It requires thunk middleware to be used with Redux
const store = reducer => createStore(reducer, applyMiddleware(thunk))
Documentation
initialState before after error
Constructor parameters:
initialState: Object
- this is an initial state it will be used in reducer method TODO: before and after should accept functions toobefore: Object|Function
- optional will be dispatched on the state before the action. Good if you need to display a preloader when you want to call an async action.after: Object|Function
- optional will be dispatched on the state before the action. Good if you need to hide a preloader when you want to call an async action.error: Function
- optional will be dispatched if action will throw an error
simpleRedux.reducer
Generic reducer will dispatch all action added to SimpleState instances on the store
simpleRedux.actionFactory(type, config)
type: String
- base action type/name. Subsequent action names will be created automatically if you useerror
,before
orafter
i.e. if you action name isget/data
thenget/data/error
will be created automatically if error property is present in theconstructor
oractionFactory
config
Config parameters:
needsUpdate: (...any) => (getState: Function) => boolean
- if function will returnfalse
action won't be executed. Handy in i.e.RouteActionComponent
to prevent action infinite loop. // TODO: params should be wrapped in an Object => to be added in V5action: Object | (...any) => (getState: Function, ...thunkAdditionalParams: any, dispatch: Function) => Promise<any> | any
- a factory function or update obect. Whatherwer will be returned by action will be automatically dispatched on the store. Available parameters:(getState, /* whatever thunk */, dispatch)
and all extra params added to thunkbefore: Object|false|Function
- optional will be dispatched on the state before the action. Good if you need to display a preloader when you want to call an async action. If value isfalse
thenbefore
method from constructor config won't be dispatched.after: Object|false|Function
- optional will be dispatched on the state before the action. Good if you need to hide a preloader when you want to call an async action. If value isfalse
thenafter
method from constructor config won't be dispatched.error: (error: Function, getState: Function, ...thunkAdditionalParams: any, dispatch: Function, params:any) => Object
- optional will be dispatched if action will throw an error. If value isfalse
thenerror
method from constructor config won't be dispatched.
Examples
Simple action:
const initialState = {} const clearState = simpleRedux
Complex action:
const getData = simpleRedux
Minimal setup
const initialState = {} const simpleRedux = initialState const clearState = simpleRedux const reducer = simpleReduxreducer
Extended setup
const initialState = {} const before = load: true const after = load: false const error = error: error const simpleRedux = initialState before after error const doSomething = simpleRedux const reducer = simpleReduxreducer
Additional properties of action returned by action factory
(simpleRedux.actionFactory(/*...*/).simpleRedux
):
simpleReduxsimpleRedux = actionNames: success error: after: before: action needsUpdate before after error
Testing outside of redux scope
For testing you might want to export your action config separtately so you don't have to relly on mocking dispatch
function
const setfilter = simpleReduxconst getData = simpleRedux
Testing in redux scope