auto-claude: subtask-4-1 - Add JSDoc to useAsync.ts hook
This commit is contained in:
@@ -1,12 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Custom Hook for Async Operations
|
||||||
|
* Provides a reusable pattern for handling asynchronous operations with loading, error, and data states
|
||||||
|
*/
|
||||||
|
|
||||||
import { useState, useCallback } from 'react';
|
import { useState, useCallback } from 'react';
|
||||||
import { handleError } from '../utils/errorHandling';
|
import { handleError } from '../utils/errorHandling';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* State interface for async operations
|
||||||
|
* @template T - The type of data returned by the async operation
|
||||||
|
*/
|
||||||
interface AsyncState<T> {
|
interface AsyncState<T> {
|
||||||
|
/** The data returned from the async operation, null if not yet loaded or error occurred */
|
||||||
data: T | null;
|
data: T | null;
|
||||||
|
/** Error object if the operation failed, null otherwise */
|
||||||
error: Error | null;
|
error: Error | null;
|
||||||
|
/** Loading state indicator, true while operation is in progress */
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom hook for managing asynchronous operations
|
||||||
|
* Handles loading states, error handling, and data management for async functions
|
||||||
|
*
|
||||||
|
* @template T - The type of data returned by the async operation
|
||||||
|
* @returns Object containing:
|
||||||
|
* - data: The result of the async operation or null
|
||||||
|
* - error: Any error that occurred or null
|
||||||
|
* - loading: Boolean indicating if operation is in progress
|
||||||
|
* - execute: Function to trigger the async operation
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```tsx
|
||||||
|
* const { data, error, loading, execute } = useAsync<User>();
|
||||||
|
*
|
||||||
|
* useEffect(() => {
|
||||||
|
* execute(() => fetchUser(userId));
|
||||||
|
* }, [userId]);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
export function useAsync<T>() {
|
export function useAsync<T>() {
|
||||||
const [state, setState] = useState<AsyncState<T>>({
|
const [state, setState] = useState<AsyncState<T>>({
|
||||||
data: null,
|
data: null,
|
||||||
|
|||||||
Reference in New Issue
Block a user