useAPI

When making an API call, you can use this hook to utilise 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 ViewContext.

Parameters

Last updated