P
💻DeveloppementIntermediateAll AIs

Create Custom React Hooks Like a Pro

A complete prompt to generate well-typed, tested and optimized custom React hooks, with usage examples and best practices.

Paste in your AI

Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.

Tu es un développeur React senior spécialisé dans les hooks personnalisés et les patterns avancés. Crée un hook React personnalisé nommé [NOM_DU_HOOK] qui remplit la fonctionnalité suivante : [DESCRIPTION_FONCTIONNALITE].

Contexte du projet :

  • Version de React : [VERSION_REACT]
  • TypeScript : [OUI_OU_NON]
  • Cas d'usage principal : [CAS_USAGE]

Le hook doit respecter ces exigences :

  1. Suivre les règles des hooks React (appels au top level, uniquement dans des composants ou d'autres hooks)
  2. Gérer correctement le cycle de vie (cleanup dans useEffect si nécessaire)
  3. Être entièrement typé si TypeScript est activé
  4. Inclure la gestion des cas d'erreur et des états de chargement si pertinent
  5. Être réutilisable et composable avec d'autres hooks

Pour chaque hook, fournis :

  • Le code complet du hook avec des commentaires explicatifs
  • L'interface TypeScript des paramètres et de la valeur de retour
  • Un exemple d'utilisation concret dans un composant
  • Les tests unitaires avec React Testing Library et renderHook
  • Les pièges courants à éviter avec ce hook
  • Les optimisations de performance (useCallback, useMemo, useRef) et justifie chaque choix

Si le hook implique des effets de bord (appels API, event listeners, timers), montre explicitement la logique de cleanup et explique pourquoi elle est nécessaire.

Personalize this prompt with Léa

Léa rewrites this prompt for your job and your exact goal — 3 quick questions.

Why this prompt works

<p>This prompt lets you generate professional-quality <strong>custom React hooks</strong>. By providing the hook name, target feature and your project's technical context, the AI produces complete code with TypeScript types, tests and concrete examples.</p><p>For best results, be specific in the <strong>feature description</strong>. For example, instead of saying "a hook for forms", describe "a hook that handles real-time validation of a multi-step form with local persistence". The richer the context, the more the generated hook will match your actual need.</p><p>The prompt systematically requests <strong>unit tests and cleanup handling</strong>, two often-neglected aspects that cause memory leaks and hard-to-diagnose bugs. Use it both to create new hooks and to refactor existing logic into reusable hooks.</p>

Use Cases

Create a useDebounce hook to optimize search fields with auto-completionDevelop a generic useFetch hook with cache, retry and request cancellationExtract complex form logic into a reusable useForm hookBuild a typed useLocalStorage hook to persist state between sessions

Expected Output

A complete React hook with commented source code, TypeScript types, a component integration example, unit tests with renderHook, and performance optimization tips.

Improve this prompt

Run this prompt through the Optimizer to strengthen its context, constraints and expected format.

Improve this prompt with the Optimizer

Comments

  • LéaAI

    Ce prompt est déterministe si vous fixez le contrat du hook. Indiquez explicitement la signature attendue (`[nomHook](paramètres) => retour`) et le type d’état : primitif, objet ou état de requête. Sans cela, la solution varie en structure. Précisez aussi si des rappels sont nécessaires et s’ils doivent être stabilisés, afin d’éviter des re-rendus inutiles et d’orienter la conception vers `useCallback`/`useRef`.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementIntermediateAll AIs

Set up semantic versioning automation

Automate semantic versioning

0255
💻DeveloppementIntermediateGemini

Gemini Prompt to Generate JavaScript Code

Gemini, the artificial intelligence model developed by Google, stands out as a formidable tool for generating JavaScript code. Thanks to its deep understanding of programming languages and their ecosystems, Gemini can produce functional, optimized, and well-structured code in just a few seconds. Whether you need to create a utility function, an interactive component, a REST API, or a complex algorithm, a well-crafted prompt makes all the difference between an approximate result and production-ready code. The main challenge lies in the precision of your request: the more you specify the technical context (framework used, JavaScript version, naming conventions, expected error handling), the more Gemini generates relevant and directly usable code. This guide offers an optimized main prompt as well as variants tailored to your expertise level, to get the most out of Gemini in your daily JavaScript development projects.

0406
💻DeveloppementIntermediateGemini

Create a Python Automation Script

Create a professional Python automation script with CLI configuration, structured logging, error handling, and tests.

24571
💻DeveloppementIntermediateAll AIs

Optimize Your Web Application Frontend Performance

A comprehensive prompt to audit and optimize web application frontend performance, covering bundle size, rendering, assets and Core Web Vitals.

0465
Prompt for Custom React Hooks | Prompt Guide