Gathering detailed insights and metrics for redux-motive
Gathering detailed insights and metrics for redux-motive
Gathering detailed insights and metrics for redux-motive
Gathering detailed insights and metrics for redux-motive
Simplify writing action creators, reducers and effects - without breaking redux.
npm install redux-motive
Typescript
Module System
Node Version
NPM Version
71.5
Supply Chain
98.4
Quality
75.6
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
5,881
Last Day
2
Last Week
29
Last Month
51
Last Year
715
8 Stars
52 Commits
5 Watchers
76 Branches
3 Contributors
Updated on May 31, 2023
Minified
Minified + Gzipped
Latest Version
0.5.0
Package Id
redux-motive@0.5.0
Unpacked Size
31.00 kB
Size
6.93 kB
File Count
5
NPM Version
6.5.0
Node Version
10.12.0
Cumulative downloads
Total Downloads
Last Day
0%
2
Compared to previous day
Last Week
866.7%
29
Compared to previous week
Last Month
-43.3%
51
Compared to previous month
Last Year
22.2%
715
Compared to previous year
20
Simplify writing action creators, reducers and effects - without breaking redux.
1const { reducer, ...actionCreators } = ReduxMotive({ 2 config: {}, 3 sync: { 4 // Sync function, combines Action Creator and Reducer 5 addTodo (state, todo) { 6 return assign({}, state, { todos: [ ...state.todos, todo ] }) 7 }, 8 }, 9 async: { 10 // Async function, combines Action Creator and Effect 11 async createTodo (motive, text, isDone) { 12 const todo = await api('/todo', {text, isDone}) 13 motive.addTodo(todo) 14 } 15 }, 16})
1yarn add redux-motive
Add redux-thunk
to your store's middleware. See redux-thunk
docs for more details.
1yarn add redux-thunk
1import thunk from 'redux-thunk' 2const store = createStore(reducers, applyMiddleware(thunk))
In UI development, our motive's for using redux are predictable.
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.
Generate action creators and a reducer with Motive.
1const { reducer, ...actionCreators } = ReduxMotive({ 2 sync: { 3 // Sync function, combines Action Creator and Reducer 4 addTodo (state, todo) { 5 return assign({}, state, { todos: [ ...state.todos, todo ] }) 6 }, 7 }, 8 async: { 9 // Async function, combines Action Creator and Effect 10 async createTodo (motive, text, isDone) { 11 const todo = await api('/todo', {text, isDone}) 12 motive.addTodo(todo) 13 } 14 }, 15})
Write action types, action creators and reducers with common redux boilerplate.
1const ADD_TODO = '@@MOTIVE/ADD_TODO' 2const CREATE_TODO_START = '@@MOTIVE/CREATE_TODO_START' 3const CREATE_TODO_END = '@@MOTIVE/CREATE_TODO_END' 4const CREATE_TODO_ERROR = '@@MOTIVE/CREATE_TODO_ERROR' 5 6const reducer = (state, action) => { 7 switch (action.type) { 8 case ADD_TODO: 9 return assign({}, state, { todos: [ ...state.todos, todo ] }) 10 case CREATE_TODO_START: 11 return assign({}, state, { progressing: true }) 12 case CREATE_TODO_END: 13 return assign({}, state, { progressing: false }) 14 case CREATE_TODO_ERROR: 15 return assign({}, state, { error: action.payload, progressing: false }) 16 } 17} 18 19const actionCreators = { 20 addTodo (todo) { 21 return { type: ADD_TODO, payload: { todo } } 22 }, 23 24 createTodo (text, isDone) { 25 return (dispatch) => { 26 dispatch({ type: CREATE_TODO_START }) 27 api('/todo', {text, isDone}) 28 .then(todo => { 29 dispatch(actionCreators.addTodo(todo)) 30 dispatch({ type: CREATE_TODO_END }) 31 }) 32 .catch(err => { 33 dispatch({ type: CREATE_TODO_ERROR, payload: err }) 34 }) 35 } 36 } 37}
Inferring common redux patterns into ReduxMotive
allows for less coding.
ReduxMotive
always does behind the scenes.ReduxMotive
is reduced to state at common stages: start, end or error.ReduxMotive
provides dispatch
-bound Action Creators in an effect's first parameter.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.
1const motive = ReduxMotive({ 2 config: {} 3 sync: { 4 todo () {}, 5 }, 6 async: { 7 async fetchTodo () {} 8 } 9}); 10 11console.log(motive); 12// { 13// reducer, Reducer function, wrapping all configured sync fns 14// todo, An Action Creator generated from sync.todo 15// fetchTodo An Action Creator generated from async.fetchTodo 16// }
config
Initial state, default handlers for state/end/error, and optional prefix for action types.
1 2ReduxMotive({ 3 // Default config values 4 config: { 5 prefix: '', 6 initialState: {}, 7 handlers: { 8 start: (state) => assign({}, state, { progressing: true }), 9 end: (state) => assign({}, state, { progressing: false }), 10 error: (state, error) => assign({}, state, { progressing: false, error }) 11 }, 12 } 13})
sync
A collection of functions that combine the principles of an Action Creator and a Reducer.
They should:
1const { todo } = ReduxMotive({ 2 sync: { 3 todo (state, isDone) { 4 return { ...state, isDone } 5 } 6 } 7}) 8 9dispatch( todo(true) )
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
ReduxMotive
, bound to dispatch
1ReduxMotive({ 2 // ... 3 4 async: { 5 async fetchTodo (motive) { 6 const todo = await api(); 7 motive.todo(todo.isDone) 8 } 9 } 10})
Lifecycles for an Async Function
Refer to the Comparison for when 'lifecycle' stages are actioned and reduced.
The stages can be overridden:
config
1ReduxMotive({ 2 config: { 3 handlers: { /* ... */ } 4 }, 5 6 async: { 7 fetchTodo: { 8 handlers: { 9 start (state) { /* ... */ }, 10 end (state) { /* ... */ }, 11 error (state) { /* ... */ } 12 }, 13 async effect (motive) { 14 const todo = await api(); 15 motive.todo(todo.isDone) 16 } 17 } 18 } 19})
Action types for each Action Creators are available as properties, which is useful when attempting to match the types in a explicit way.
1console.log(motive.todo.ACTION_TYPE) 2// @@MOTIVE/<PREFIX>/TODO_SYNC 3 4console.log(motive.fetchTodo.ACTION_TYPE_START) 5// @@MOTIVE/<PREFIX>/SYNC_TODO_START 6console.log(motive.fetchTodo.ACTION_TYPE_END) 7// @@MOTIVE/<PREFIX>/SYNC_TODO_END 8console.log(motive.fetchTodo.ACTION_TYPE_ERROR) 9// @@MOTIVE/<PREFIX>/SYNC_TODO_ERROR
You don't need to use these if you're dispatching the generated Action Creators.
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. |
Licensed under the MIT License, Copyright © 2017-present Lochlan Bunn.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/20 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
63 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More