Skip to main content

useApiRequest

Makes GET requests to urls.apiUrl with optional periodic repetition.

Usage​

import { useApiRequest } from '@machinemetrics/mm-react-tools';

function MyWidget() {
const { data, error, loading } = useApiRequest(
'/machines',
null,
5000, // Optional: refresh every 5 seconds
);

if (loading) return <div>Loading metrics...</div>;
if (error) return <div>Error loading metrics: {error.message}</div>;

return (
<div>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}

Parameters​

ParameterTypeDescription
endpointstringThe API endpoint to fetch from (will be prefixed with urls.apiUrl)
optionsRequestInitOptional fetch options (headers, method, body, etc.)
refreshIntervalnumberOptional interval in milliseconds to refetch the data

Returns​

Returns an object containing:

  • data: The response data (null if not yet loaded)
  • error: Any error that occurred during the request (null if successful)
  • loading: Boolean indicating if the request is in progress