Type of data returned by the fetch function
Synchronous function to fetch data
Dependency array for useEffect (when to re-fetch)
Service data state with loading, error, data, and refetch function
// Basic usage with service call
const { data, loading, error, refetch } = useServiceData(
() => securityMetricsService.getMetrics(level),
[level]
);
if (loading) return <LoadingSpinner />;
if (error) return <ErrorMessage error={error} retry={refetch} />;
if (!data) return <NoDataMessage />;
return <DataDisplay data={data} />;
Custom hook for fetching service data with loading and error states
Business Perspective
Standardizes data fetching across all widgets, ensuring consistent loading states and error handling. This improves user experience by providing predictable feedback during data operations and simplifies maintenance by centralizing error handling logic. 🔄
Technical Perspective
Extracts the common loading/error/data pattern found in 6+ widgets, reducing code duplication and ensuring consistent error handling. Uses React hooks best practices with proper dependency management.
Note: This hook currently supports synchronous fetch functions only. The services in this application return data synchronously, so this design choice simplifies the implementation and testing. For async operations, the hook can be extended in the future if needed.