Build a Reusable React Custom Hook with TypeScript and State Management

Generate a fully typed React custom hook with state management, error handling, cleanup, and comprehensive test cases.

๐Ÿ“ The Prompt

Create a reusable React custom hook called `use[HOOK_NAME]` in TypeScript that handles [HOOK_PURPOSE] (e.g., form management, API data fetching, infinite scroll, debounced search, WebSocket connection). Hook Requirements: - Input parameters: [INPUT_PARAMETERS_WITH_TYPES] - Return values: [EXPECTED_RETURN_VALUES] - Must handle these states: loading, error, success, and [ADDITIONAL_STATES] Please implement the following: 1. **Hook Signature & Types**: Define a clear TypeScript interface for the hook's options parameter and return type. Use generics where the data type varies (e.g., `use[HOOK_NAME]<T>`). 2. **Core State Management**: Use appropriate React hooks (`useState`, `useReducer`, `useRef`) to manage internal state. If the state logic is complex, implement a `useReducer` with typed actions and a reducer function. 3. **Side Effects**: Implement `useEffect` for [SIDE_EFFECT_DESCRIPTION] with proper: - Dependency array management - Cleanup function to prevent memory leaks - AbortController for cancellable operations if applicable 4. **Memoization**: Apply `useCallback` and `useMemo` where appropriate to prevent unnecessary re-renders. Explain each memoization decision in comments. 5. **Error Handling**: Implement error boundaries compatibility, retry logic with [RETRY_STRATEGY] (e.g., exponential backoff, max 3 attempts), and error state recovery. 6. **Configuration Options**: Support these configurable options with sensible defaults: - [OPTION_1]: [DEFAULT_VALUE_1] - [OPTION_2]: [DEFAULT_VALUE_2] - [OPTION_3]: [DEFAULT_VALUE_3] 7. **Usage Example**: Provide a complete example component demonstrating the hook in a realistic scenario with [USE_CASE_SCENARIO], showing all return values being consumed. 8. **Testing**: Write 3 test cases using React Testing Library and `renderHook` covering: initial state, successful operation, and error handling.

๐Ÿ’ก Tips for Better Results

Be specific about [EXPECTED_RETURN_VALUES] โ€” list exact property names and types you want returned so the hook's API matches your component needs. Mention your React version if you want to leverage newer features like useOptimistic or use() hook. Providing a real [USE_CASE_SCENARIO] for the usage example ensures the output is immediately applicable to your project.

๐ŸŽฏ Use Cases

React developers use this when extracting repeated stateful logic into shared hooks across components. Perfect for team leads establishing reusable patterns in a component library or design system.

๐Ÿ”— Related Prompts

๐Ÿ’ป Coding beginner

Explain Code Like Im a Beginner

Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.

๐Ÿ’ป Coding beginner

Debug My Code and Explain the Fix

Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.

๐Ÿ’ป Coding intermediate

Write Unit Tests for My Code

Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.

๐Ÿ’ป Coding intermediate

Convert Code Between Languages

Convert code between any programming languages while maintaining idiomatic patterns and best practices.

๐Ÿ’ป Coding intermediate

Write a REST API Endpoint

Generate production-ready REST API endpoints with validation, error handling, and documentation.

๐Ÿ’ป Coding advanced

Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment

Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.