following-the-rules-of-hooks

Detect and fix React Rules of Hooks violations in JavaScript and TypeScript codebases.

Updated Nov 21, 2025
One-click install
npx skills add https://github.com/djankies/claude-configs --skill following-the-rules-of-hooks
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: following-the-rules-of-hooks
Source: https://github.com/djankies/claude-configs/tree/main/react-19/skills/following-the-rules-of-hooks
Command: npx skills add https://github.com/djankies/claude-configs --skill following-the-rules-of-hooks

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Violating React Rules of Hooks (calling hooks inside loops, conditions, or non-component functions) leads to unstable state, bugs, and inconsistent render orders.

Core Features & Use Cases

  • Detects violations like hooks inside conditional blocks or loops.
  • Provides fixes by hoisting hooks to the top level and moving conditional logic into render-time guards.
  • Guidance for custom hooks and avoiding hooks inside class components.

Quick Start

Audit components for hook usage in conditions/loops and refactor to call hooks at the top level; push conditional logic into rendering instead of conditional hooks.

Frequently Asked Questions about following-the-rules-of-hooks

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix React hooks being called inside conditions or loops?

React Rules of Hooks require all hooks to be called at the top level of components, never inside conditions or loops. Move conditional logic into the component body or use hooks like useEffect with dependency arrays to guard execution, keeping hook invocations unconditional.

Why does React throw an error when I call useState or useEffect inside an if statement?

Hooks rely on consistent call order across renders. Conditional hooks change that order, causing React to misalign state with hook invocations and producing unstable state or render bugs. Hoist hooks to the top level and move conditions into render-time guards instead.

Can I use React hooks inside callbacks or event handlers?

No. Hooks must be called only from React components or custom hooks at the top level. Calling hooks inside callbacks, event handlers, or regular functions violates Rules of Hooks. Extract hook logic into your component body or a custom hook, then call that from your handler.

How do I refactor custom hooks to follow React Rules of Hooks?

Custom hooks must call other hooks at the top level without conditions or loops, the same as components. Design custom hooks to perform one focused task, initialize all state and side effects unconditionally, and return values or handlers that manage conditional behavior downstream.

What's the difference between violating Rules of Hooks in class components versus function components?

Hooks don't work in class components at all. Rules of Hooks apply to function components and custom hooks. Use lifecycle methods (componentDidMount, componentDidUpdate) in class components instead, or refactor to function components to use hooks.