Simplify writing action creators, reducers and effects - without breaking redux.
const reducer ...actionCreators =
Install
yarn add redux-motive
Requirements
Add redux-thunk
to your store's middleware. See redux-thunk
docs for more details.
yarn add redux-thunk
const store =
Preamble
In UI development, our motive's for using redux are predictable.
- Reduce an Action to change the state now, to rerender the UI soon.
- Reduce the lifecycle of side effects, from an Action, to change state over time, to rerender the UI as the side effects progress.
Redux is great for splitting data-flow concerns into small concepts, but it can introduce indirection to a developers code, and at times this becomes the source of errors.
Motive removes indirection, by combining the purpose of a data-flow function to be both an Action Creator and a Reducer, or an Action Creator and an Effect.
Comparison
Generate action creators and a reducer with Motive.
const reducer ...actionCreators =
Write action types, action creators and reducers with common redux boilerplate.
const ADD_TODO = '@@MOTIVE/ADD_TODO'const CREATE_TODO_START = '@@MOTIVE/CREATE_TODO_START'const CREATE_TODO_END = '@@MOTIVE/CREATE_TODO_END'const CREATE_TODO_ERROR = '@@MOTIVE/CREATE_TODO_ERROR' const reducer = { } const actionCreators = { return type: ADD_TODO payload: todo } { return { } }
Summary
Inferring common redux patterns into ReduxMotive
allows for less coding.
- Action Creators often pass their params to Reducers in the Action;
ReduxMotive
always does behind the scenes. - The progress of an effect's lifecycle in
ReduxMotive
is reduced to state at common stages: start, end or error. - Dispatching actions from the end of effects is guaranteed;
ReduxMotive
providesdispatch
-bound Action Creators in an effect's first parameter.
API
ReduxMotive( { config, sync, async } )
The returned object can be used to provide a reducer
to the Redux.
Additionally, every function configured for sync
and async
are accessible as dispatchable Action Creators.
const motive = ; console;// {// reducer, Reducer function, wrapping all configured sync fns// todo, An Action Creator generated from sync.todo// fetchTodo An Action Creator generated from async.fetchTodo// }
Configuring
# config
Initial state, default handlers for state/end/error, and optional prefix for action types.
# sync
A collection of functions that combine the principles of an Action Creator and a Reducer.
They should:
- Always return new state
- Should not call any "side effects"
const todo =
# async
Combination of an Action Creator and an Effect.
Function that is given a motive
Object and any additional arguments from the generated Action Creator.
Expected to dispatch new Actions from invoke side effects (like server API calls).
Should return a Promise. The async
function keyword can be used.
motive
Object
dispatch
getState
- Action Creators returned by
ReduxMotive
, bound todispatch
Lifecycles for an Async Function
Refer to the Comparison for when 'lifecycle' stages are actioned and reduced.
The stages can be overridden:
- In the
config
- Per (asynchronous) function
Action Types
Action types for each Action Creators are available as properties, which is useful when attempting to match the types in a explicit way.
console// @@MOTIVE/<PREFIX>/TODO_SYNC console// @@MOTIVE/<PREFIX>/SYNC_TODO_STARTconsole// @@MOTIVE/<PREFIX>/SYNC_TODO_ENDconsole// @@MOTIVE/<PREFIX>/SYNC_TODO_ERROR
You don't need to use these if you're dispatching the generated Action Creators.
Alternatives & inspirations
Library | Description |
---|---|
redux-schemas | Similar redux util library, making different API choices, but with more utility. |
freactal | Unidirection store for React, with a concise api for async actions and selectors. |
License
Licensed under the MIT License, Copyright © 2017-present Lochlan Bunn.