Gravity
Search
⌃K

useAPI

When you need to make an API call, you can use this hook to benefit from handling your loading and error logic in one place. The API hook will update whenever one of the parameters passed has changed.
const stats = useAPI('/api/demo/stats');
return { stats?.loading ? <Loader /> : <Table data={ stats?.data }}/>
The useAPI hook returns an object with two properties: loading and data. You can use the loading value to handle the loading state in the parent object. Data will be returned when the API response is returned.
The hook will automatically forward any errors to the error handler in the view context.

Parameters

param
description
value
url
API endpoint to call
url string
method
HTTP method (default: GET)
string