Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
🎸 react-redux hook & redux middleware to be able to wait for async actions with fixed defined suffixes
xcarpentier/react-redux-dispatch-async
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
👉 REDUXmiddleware &HOOK 🎉 waiting asyncactions withSUFFIXES 👈
import{useDispatchAsync}from'react-redux-dispatch-async'exportdefaultfunctionMyUserInterface({ id}:{id:string}){// 👉 pass action and arguments into the arrayconstresponse=useDispatchAsync(getUserActionRequest,[id])switch(response.status){case'loading':return<AppLoader/>case'error':return<Text>{response.error.message}</Text>case'success':return<User{...response.result}/>case'timeout':return<Text>{'timeout ¯\\_(ツ)_//¯'}</Text>case'canceled':return<Text>{'canceled ¯\\_(ツ)_//¯'}</Text>default:returnnull}}
If you need more examples you can go togithub or tocodesandbox.
yarn add react-redux-dispatch-async
+------------------+ | ACTION_REQUESTED |----+ +------------------+ | +------------------+ +----->| ACTION_SUCCEEDED | | +------------------+ | | +--------------------+ +----->| ACTION_FAILED | | +--------------------+ | | +--------------------+ +----->| ACTION_CANCELED | +--------------------+- ⏳
loading: action start but not yet completed - 👏
success: action completed, you can get the result - 😱
error: action failed and you can get the error - 👎
timeout: action not completed for too long (ie. options?.timeoutInMilliseconds) - 👋
canceled: action canceled - 😮
unknown: should never happen
import{createStore,applyMiddleware}from'redux'import{createDispatchAsyncMiddleware}from'react-redux-dispatch-async'importreducersfrom'reducers'conststore=createStore(reducers,applyMiddleware(createDispatchAsyncMiddleware({request:'REQUEST',// 👈 define your own async suffixessuccess:'SUCCESS',failure:'FAILURE',cancel:'CANCEL',// optional}),),)
[...]_REQUESTED[...]_SUCCEEDED[...]_FAILED[...]_CANCELED
dispatchAsyncMiddleware:(c?:{request:stringsuccess:stringfailure:stringcancel?:string})=>redux.Middleware
// main hookinterfaceOptions{timeoutInMilliseconds?:number}typeuseDispatchAsync=<R=any>(actionFunction?:(...args:any[])=>Action<T>,deps:any[]=[],options:Options={timeoutInMilliseconds:15000},// wait 15s)=>UseDispatchAsync<R>/// return typeinterfaceResultLoading{status:'loading'}interfaceResultSuccess<R=unknown>{status:'success'result:R}interfaceResultError{status:'error'error:Error}interfaceResultCancel{status:'canceled'}interfaceResultTimeout{status:'timeout'}interfaceResultUnknown{status:'unknown'}exporttypeUseDispatchAsync<R=unknown>=|ResultLoading|ResultSuccess<R>|ResultError|ResultTimeout|ResultCancel|ResultUnknown// other types for oldest usageinterfaceDispatchAsyncResultSuccess<T=any>{success:trueresult:T}interfaceDispatchAsyncResultError{success:falseerror:Error}exporttypeDispatchAsyncResult<T=any>=|DispatchAsyncResultSuccess<T>|DispatchAsyncResultError
Looking for a ReactNative freelance expert with more than 14 years experience? Contact me from my website!
About
🎸 react-redux hook & redux middleware to be able to wait for async actions with fixed defined suffixes
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.