React Promise Switch ·
React Promise Switch abstracts the overhead and complexity of using promises to store and render data in a component's state.
Example
import axios from "axios";import usePromise from "react-promise-switch"; const fetchUsers = axios; { const err users requestStatus = ; if !users return "Loading..."; return <UsersList = />;}
Usage
usePromiseSwitch
accepts a function that returns a promise and an optional object of options:
fn
Type: <T>() => Promise<T>
A function that returns a promise. When fn
's reference changes, usePromise
will cancel the old request and start the new one.
Examples
const getUsers = Promise; { const err users = ; return <pre>users ? JSON : null</pre>;}
If you define fn
inside of a React component, make sure to memoize the function so its reference doesn't change on re-render:
{ const err users = ; return <pre>users ? JSON : null</pre>;}
Options
mode
Type: "latest" | "every"
Default: "latest"
Determines whether to show only the most recent promise (latest
) or to show the most recently resolved promise in the queue (every
). A typical usecase for every
is when implementing an "auto-complete" search, where you want to show the latest result as they stream in.
cancel
Type: ?(Promise) => void
Default: void
Although usePromise
will cancel any provided promise out of the box, it can be helpful to integrate cancelation more deeply when using libraries like Bluebird or Axios. The function you provide will be called whenever usePromise
determines cancelation is needed (such as during unmount, changing the promise before the first one has completed, etc.)
Example (Axios):
You need a way to reference the cancel function from Axios's CancelToken
. One common way to approach this is to add a .cancel
method to the promise returned by Axios:
; const getUsers = { const source = CancelTokensource; const promise = axios; promise source; return promise;}
You can then call it in the cancel
option for usePromise
:
;
If you wrap all of your Axios promises this way and often use usePromise
throughout your app, it can be helpful to preconfigure this option:
; const useAxiosPromise = ;
More Examples
Waiting to Fetch Data
Sometimes you don't want to trigger the promise right away, but instead want to wait until some action has occurred. You can pass null
to usePromise
and it will remain in the "INITIAL"
state:
const submitData = axios; { const submitted setSubmitted = React; const err result status = ; return <button = =>Submit</button>;}
Providing Arguments
Sometimes you may want to pass arguments to the function provided to usePromise
. You should wrap the function call with a React.useCallback
so that it only re-fetches when the arguments change.
const getUser = id axios; { const userId setUserId = React; const err user = return <div> <UserSelector = = /> <UserProfile = />; </div> ;}
Handling Side Effects
If you need to handle side effects after the state of the promise changes, you can use React.useEffect
.
const submitForm = formState axios; { const submitted setSubmitted = React; const formState setFormState = React; const formErrors successful status = ; React; return <Form = = = = />}