Gravity
Search…
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.
1
const stats = useAPI('/api/demo/stats');
2
return { stats?.loading ? <Loader /> : <Table data={ stats?.data }}/>
Copied!
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
Copy link
Contents
Parameters