• Hook that performs an api request via a get method on an API service and automatically handles loading and error states. Use this hook when you want to fetch data from an API.

    Data will automatically refresh when the params change. Note that params are compared using strict equality, so if you pass an object, it will only refetch if the object reference changes.

    Type Parameters

    • TResponse

    • TParams

    Parameters

    • apiMethod: ((params) => Promise<TResponse>)

      The method to call on the API service

    • params: undefined | TParams

      The parameters to pass to the API method (can be undefined if skip is true)

    • options: ApiGetOptions = {}

      Options for the hook

      • errorBehavior: 'throw' to throw the error (crashing the app; default behavior), 'return' to return the error in the state
      • skip: Set to true to skip the api call. This is useful when you want to conditionally run this api call (such as when the component changes states)

    Returns [ApiGetState<TResponse>, (() => Promise<void>)]

    A tuple containing the state of the api call and a function to refetch the data manually if needed

    Note

    Make sure to use .bind() when passing an API method to this hook to ensure that the method can access .this

Generated using TypeDoc