0.1.7 • Published 4 years ago
use-async-machine v0.1.7
use-async-machine
A hook for using an async state machine. The state machine this library implements looks like this (image generated with XState Visualizer):
Usage
This library exports a single hook to help you with asynchronous operations in your UI.
import React from 'react'
import useAsync from 'use-async-machine'
function someAsyncTask() {
// Returns a promise
}
export default function App() {
const { data, error, isLoading, isError, dispatch } = useAsync(() => someAsyncTask())
React.useEffect(() => {
dispatch({ type: 'load' })
}, []) // 'dispatch' does not need to be included in dependency array
if (isLoading) {
return <Spinner />
}
if (isError) {
return <ErrorMessage error={error}>
}
return <div>Here's the result: {data}</div>
}Use Cases
This library was created out of a need to generate and render a PDF document in the browser. useAsync is most useful for client-only asynchronous tasks such as this. If you need to interact with remote data sources, I recommend you try a data caching library such as react-query or use an SSR framework such as Remix.
API Reference
const {
state,
data,
error,
context,
isIdle,
isLoading,
isError,
isSuccess,
dispatch,
} = useAsync(fn, {
onLoad,
onSuccess,
onError,
});Options
fn: (state: UseAsyncState<TData, TError, TContext>) => Promise<TData>- The asynchronous function to run on the
loadevent. - Receives the async state.
- Must return a promise that either resolves to data or throws an error.
- The asynchronous function to run on the
onLoad: (state: UseAsyncState<TData, TError, TContext>) => unknown- Function to be called when the state machine enters the
loadingstate. - Receives the async state
- Returns nothing
- Function to be called when the state machine enters the
onSuccess: (state: UseAsyncState<TData, TError, TContext>) => unknown- Function to be called when the state machine enters the
successstate. - Receives the async state
- Returns nothing
- Function to be called when the state machine enters the
onError: (state: UseAsyncState<TData, TError, TContext>) => unknown- Function to be called when the state machine enters the
errorstate. - Receives the async state
- Returns nothing
- Function to be called when the state machine enters the
Returns
state: "idle" | "loading" | "success" | "error"- Defaults to
"idle" - The state of the async function.
- Defaults to
data: TData- Defaults to
undefined - The resolved data returned by
fn
- Defaults to
error: TError- Defaults to
undefined - If
fnrejects with an error, it will be stored here
- Defaults to
context: TContext- Defaults to
undefined - May be set by the
"load"event withdispatch({ "type": "load", context }) - Useful if you want to pass additional context to
fn
- Defaults to
isIdle: boolean- Derived from
state, true if and only ifstate === "idle"
- Derived from
isLoading: boolean- Derived from
state, true if and only ifstate === "loading"
- Derived from
isError: boolean- Derived from
state, true if and only ifstate === "error"
- Derived from
isSuccess: boolean- Derived from
state, true if and only ifstate === "success"
- Derived from
dispatch: React.Dispatch<UseAsyncEvent<TData, TError, TContext>>- Dispatch function returned from
React.useReducer - Used to transition between async states.
- Dispatch function returned from