Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create redux reducers for async behaviors of multiple actions.

License

NotificationsYou must be signed in to change notification settings

AndersDJohnson/redux-reducer-async

Repository files navigation

Create redux reducers for async behaviors of multiple actions.

npmTravis CICodecov

Be DRY & reduce boilerplate.Standardize state schema with managed properties for loading, success, and error cases.

Think of it asredux-actions for asynchronous reducers.

Works withFlux Standard Actions (FSA).By default, supports the action type conventions of redux-promise-middleware,but seeCustom Action Types below for configuration to supportredux-promise.

Install

npm install --save redux-reducer-async (copy)

Use

importcreateReducerfrom'redux-reducer-async'constmyActionReducer=createReducer('MY_ACTION')

results in a reducer like this:

(state={},action={})=>{switch(action.type){case'MY_ACTION_PENDING':return{ ...state,loading:true,error:null}case'MY_ACTION_FULFILLED':return{ ...state,loading:false,error:null,data:action.payload}case'MY_ACTION_REJECTED':return{ ...state,loading:false,error:action.payload}default:returnstate}}

You can then mount it withcombineReducers:

import{combineReducers}from'redux'importcreateReducerfrom'redux-reducer-async'constrootReducer=combineReducers({myAction:createReducer('MY_ACTION')})

Or even call it manually within another reducer (useful withcustom properties orreducers):

importcreateReducerfrom'redux-reducer-async'constmyActionReducer=createReducer('MY_ACTION')constreducer=(state={},action={})=>{state=myActionReducer(state,action)// ...returnstate}

Custom Properties

You can provide custom property names (all optional) for each case to be used on the state:

createReducer('MY_ACTION',{loading:'isMyActionLoading',success:'myActionData',error:'myActionError'})

Custom Reducers

You can also provide custom reducer functions (again all optional, but be careful to define all cases if you use non-standard property names in one):

createReducer('MY_ACTION',{loading:state=>({    ...state,myActionError:null,myActionIsLoading:true,extra:'whatever'})// success, error...})

And you can even mix these with custom properties:

createReducer('MY_ACTION',{loading:'isLoading',error:(state,action)=>({    ...state,isLoading:false,error:action.payload,also:'etc'})})

Custom Action Types

You can provide custom action types.

For example, to supportredux-promise, which uses same the action type for success and error cases (though it does not provide a loading action),you can usefinalActionType:

importcreateReducer,{finalActionType}from'redux-reducer-async'createReducer(finalActionType('MY_ACTION'))

which is effectively like providing custom action types:

createReducer({loading:'MY_ACTION_PENDING',success:'MY_ACTION',error:'MY_ACTION'})

Or similarly by passing suffixes to theactionTypes helper,which is normally used to explicitly define all types:

importcreateReducer,{actionTypes}from'redux-reducer-async'createReducer(actionTypes('MY_ACTION','_LOADING','_SUCCESS','_ERROR'))

But can also be used to suppress suffixes (here undefined means use default):

createReducer(actionTypes('MY_ACTION',undefined,'',''))

Transforms

As a shortcut to defining custom reducers, you can provide transform functions to manipulate only the payload, optionally in success and/or error cases:

createReducer('MY_ACTION',{transform:payload=>({    ...payload,title:payload.title.trim()}),transformError:payload=>({    ...payload,message:`There was an error:${payload.message}`})})

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp