React Use API
  • Introduction
  • Usage
    • Getting Started
    • Advanced Usage
      • List API and Pagination
      • Request Control
      • Data Processing
      • Side-Effect Dependencies
      • Watch option
    • TypeScript
  • API
    • useApi
    • State
    • Request Function
    • ApiProvider
    • injectSSRHtml
    • loadApiCache
  • Server Side Rendering
    • SSR Settings
    • Examples
      • Real World Examples
      • With Ant Design
Powered by GitBook
On this page
  • Code
  • Types
  • Config
  • Options [Optional]

Was this helpful?

  1. API

useApi

PreviousTypeScriptNextState

Last updated 5 years ago

Was this helpful?

Code

const [data, state, request] = useApi(config, options)

Types

const [data, state, request] = useApi(
  config: ReactUseApi.Config | string,
  opt?: ReactUseApi.Options | ReactUseApi.Options['handleData']
)

Config

The config can be an or a URL string.

Options [Optional]

Name

Type

default

Description

handleData

Function(data: any, state: ReactUseApi.State)

A callback function to deal with the data of the API's response. IMPORTANT Using any state setter in handleData is dangerous, which will cause the component re-rendering infinitely while SSR rendering.

dependencies

Object

The additional needed data using in handleData. NOTE: "dependencies" is supposed to immutable due to React's rendering policy.

shouldRequest

Function

A callback to decide whether useApi re-fetches the API when re-rendering. Returning true will trigger useApi to re-fetch. This option is helpful if you want to re-request an API when a route change occurs.

watch

any[]

[]

An array of values that the effect depends on, this is the same as the second argument of useEffect.

Axios Request Config