Skip to content

๐ŸŽธ react-redux hook & redux middleware to be able to wait for async actions with fixed defined suffixes

Notifications You 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 version npm downloads

react-redux-dispatch-async

๐Ÿ‘‰ REDUX middleware & HOOK ๐ŸŽ‰ waiting async actions with SUFFIXES ๐Ÿ‘ˆ

import { useDispatchAsync } from 'react-redux-dispatch-async'

export default function MyUserInterface({ id }: { id: string }) {
  // ๐Ÿ‘‰ pass action and arguments into the array
  const response = 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:
      return null
  }
}

If you need more examples you can go to github or to codesandbox.

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'
import reducers from 'reducers'

const store = createStore(
  reducers,
  applyMiddleware(
    createDispatchAsyncMiddleware({
      request: 'REQUEST', // ๐Ÿ‘ˆ define your own async suffixes
      success: 'SUCCESS',
      failure: 'FAILURE',
      cancel: 'CANCEL', // optional
    }),
  ),
)

Default suffixes

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

Two functions

Configuration

dispatchAsyncMiddleware: (c?: {
  request: string
  success: string
  failure: string
  cancel?: string
}) => redux.Middleware

Type

// main hook
interface Options {
  timeoutInMilliseconds?: number
}
type useDispatchAsync = <R = any>(
  actionFunction?: (...args: any[]) => Action<T>,
  deps: any[] = [],
  options: Options = { timeoutInMilliseconds: 15000 }, // wait 15s
) => UseDispatchAsync<R>

/// return type
interface ResultLoading {
  status: 'loading'
}

interface ResultSuccess<R = unknown> {
  status: 'success'
  result: R
}

interface ResultError {
  status: 'error'
  error: Error
}

interface ResultCancel {
  status: 'canceled'
}

interface ResultTimeout {
  status: 'timeout'
}

interface ResultUnknown {
  status: 'unknown'
}

export type UseDispatchAsync<R = unknown> =
  | ResultLoading
  | ResultSuccess<R>
  | ResultError
  | ResultTimeout
  | ResultCancel
  | ResultUnknown

// other types for oldest usage
interface DispatchAsyncResultSuccess<T = any> {
  success: true
  result: T
}
interface DispatchAsyncResultError {
  success: false
  error: Error
}
export type DispatchAsyncResult<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