Hooks
useSchema
A custom React hook for fetching and managing function schemas from the backend API.
Signature
Return Value
Returns an object with three properties:
| Property | Type | Description |
|---|---|---|
schemas |
FunctionSchema[] |
Array of function schemas loaded from the backend |
loading |
boolean |
Indicates whether schemas are currently being fetched |
error |
string \| null |
Error message if fetch failed, null otherwise |
Behavior
The hook automatically fetches schemas when the component mounts using the useEffect hook:
- Sets
loadingtotrueat the start of the fetch - Calls
api.getSchemas()to retrieve schemas from the backend - Updates
schemasstate with the fetched data on success - Sets
errorstate with error message on failure - Sets
loadingtofalsewhen complete (success or failure)
Error Handling
Errors are caught and handled gracefully:
- If the error is an Error instance, uses its message
- Otherwise, uses a generic "Failed to fetch schemas" message
- Errors are also logged to the console for debugging
Usage Example
import { useSchema } from '../hooks/useSchema';
function MyComponent() {
const { schemas, loading, error } = useSchema();
if (loading) {
return <div>Loading schemas...</div>;
}
if (error) {
return <div>Error: {error}</div>;
}
return (
<div>
{schemas.map(schema => (
<div key={schema.name}>{schema.name}</div>
))}
</div>
);
}
Dependencies
- Internal:
api.getSchemas()from../utils/api - External: React's
useStateanduseEffecthooks - Types:
FunctionSchemafrom../types/schema
State Updates
The hook manages three pieces of state internally:
- schemas: Initially an empty array
[] - loading: Initially
true - error: Initially
null
When to Use
Use this hook when you need to: - Display available function nodes in the UI - Populate a node library or selection menu - Validate available functions before graph creation - Show function metadata to users
Limitations
- Fetches schemas only once on mount (no refetch mechanism)
- No caching between component unmounts
- No manual refresh capability
- Assumes API endpoint is available at component mount time