LogoLogo
HomeDocsGitHubDiscord
master
master
  • Introduction
  • Getting started
    • Installation
    • Upgrading
    • Usage
    • DevTools
  • API
    • Interfaces
    • Configuration options
    • State properties
    • Helper components
  • Guide
    • Async components
    • Separating view and logic
    • Async actions
    • Optimistic updates
    • Server-side rendering
  • Contributing
    • Introduction
    • Setting up
    • Development
    • Testing
    • Releasing
Powered by GitBook
On this page
  • data
  • error
  • value
  • initialValue
  • startedAt
  • finishedAt
  • status
  • isInitial
  • isPending
  • isFulfilled
  • isRejected
  • isSettled
  • counter
  • promise
  • run
  • reload
  • cancel
  • setData
  • setError

Was this helpful?

  1. API

State properties

PreviousConfiguration optionsNextHelper components

Last updated 5 years ago

Was this helpful?

These are returned in an object by useAsync() or provided by <Async> as render props to the children function:

  • Last resolved promise value, maintained when new error arrives.

  • Rejected promise reason, cleared when new data arrives.

  • The value of data or error, whichever was last updated.

  • The data or error that was provided through the initialValue prop.

  • When the current/last promise was started.

  • When the last promise was fulfilled or rejected.

  • One of: initial, pending, fulfilled, rejected.

  • true when no promise has ever started, or one started but was cancelled.

  • true when a promise is currently awaiting settlement. Alias: isLoading

  • true when the last promise was fulfilled. Alias: isResolved

  • true when the last promise was rejected.

  • true when the last promise was fulfilled or rejected (not initial or pending).

  • The number of times a promise was started.

  • A reference to the internal wrapper promise, which can be chained on.

  • Invokes the deferFn.

  • Re-runs the promise when invoked, using any previous arguments.

  • Cancel any pending promise.

  • Sets data to the passed value, unsets error and cancels any pending promise.

  • Sets error to the passed value and cancels any pending promise.

data

any

Last resolved promise value, maintained when new error arrives.

error

Error

Rejected promise reason, cleared when new data arrives.

value

any | Error

The data or error that was last provided (either through initialValue or by settling a promise).

initialValue

any | Error

The data or error that was originally provided through the initialValue prop.

startedAt

Date

Tracks when the current/last promise was started.

finishedAt

Date

Tracks when the last promise was resolved or rejected.

status

string

One of: initial, pending, fulfilled, rejected. These are available for import as statusTypes.

isInitial

boolean

true while no promise has started yet, or one was started but cancelled.

isPending

boolean

true while a promise is pending (loading), false otherwise.

Alias: isLoading

isFulfilled

boolean

true when the last promise was fulfilled (resolved to a value).

Alias: isResolved

isRejected

boolean

true when the last promise was rejected.

isSettled

boolean

true when the last promise was either fulfilled or rejected (i.e. not initial or pending)

counter

number

The number of times a promise was started.

promise

Promise

A reference to the internal wrapper promise created when starting a new promise (either automatically or by invoking run / reload). It fulfills or rejects along with the provided promise / promiseFn / deferFn. Useful as a chainable alternative to the onResolve / onReject callbacks.

Warning! If you chain on promise, you MUST provide a rejection handler (e.g. .catch(...)). Otherwise React will throw an exception and crash if the promise rejects.

run

function(...args: any[]): void

Runs the deferFn, passing any arguments provided as an array.

When used with useFetch, run has several overloaded signatures:

function(override: OverrideParams | (params: OverrideParams) => OverrideParams): void

function(event: SyntheticEvent | Event): void

function(): void

Where type OverrideParams = { resource?: RequestInfo } & Partial<RequestInit>.

This way you can run the fetch request with custom resource and init. If override is an object it will be spread over the default resource and init for fetch. If it's a function it will be invoked with the params defined with useFetch, and should return an override object. This way you can either extend or override the value of resource and init, for example to change the URL or set custom request headers.

reload

function(): void

Re-runs the promise when invoked, using the previous arguments.

cancel

function(): void

Cancels the currently pending promise by ignoring its result and calls abort() on the AbortController.

setData

function(data: any, callback?: () => void): any

Function that sets data to the passed value, unsets error and cancels any pending promise. Takes an optional callback which is invoked after the state update is completed. Returns the data to enable chaining.

setError

function(error: Error, callback?: () => void): Error

Function that sets error to the passed value and cancels any pending promise. Takes an optional callback which is invoked after the state update is completed. Returns the error to enable chaining.

data
error
value
initialValue
startedAt
finishedAt
status
isInitial
isPending
isFulfilled
isRejected
isSettled
counter
promise
run
reload
cancel
setData
setError