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

🎸 react-redux hook & redux middleware to be able to wait for async actions with fixed defined suffixes

NotificationsYou must be signed in to change notification settings

xcarpentier/react-redux-dispatch-async

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm versionnpm downloads

react-redux-dispatch-async

👉 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.

Install

yarn add react-redux-dispatch-async

Features

      +------------------+      | ACTION_REQUESTED |----+      +------------------+    |      +------------------+                              +----->| ACTION_SUCCEEDED |                              |      +------------------+                              |                              |      +--------------------+                              +----->|   ACTION_FAILED    |                              |      +--------------------+                              |                              |      +--------------------+                              +----->|  ACTION_CANCELED  |                                     +--------------------+

Race condition to execute only the promise if multiple update occur in nearly same time

> Dig into it

Hook give you helpful STATUS you can deal with into your own component

  • 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

Configuration

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}),),)

Default suffixes

  • [...]_REQUESTED
  • [...]_SUCCEEDED
  • [...]_FAILED
  • [...]_CANCELED

Two functions

Configuration

dispatchAsyncMiddleware:(c?:{request:stringsuccess:stringfailure:stringcancel?:string})=>redux.Middleware

Type

// 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

Hire an expert!

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

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp