Installing and Configuring the Plugin
You'll learn how to add and set up eslint-plugin-react-hooks in your project to automatically catch Hook rule violations.
Why You Need This Plugin
Remember how breaking the Rules of Hooks causes cryptic bugs? The eslint-plugin-react-hooks plugin acts like a safety net, catching violations before your code runs. It's like having a grammar checker for Hooks—it warns you immediately when you're about to break the call order invariant or use Hooks incorrectly.
Installation
First, install the plugin via npm or yarn:
npm install eslint-plugin-react-hooks --save-dev
Configuration
Open your .eslintrc file (or .eslintrc.json) and add the plugin to your configuration:
{
"plugins": ["react-hooks"],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
}
The Two Key Rules
rules-of-hooks: Enforces the two cardinal rules (top-level only, React functions only). Set this to"error"so violations block your build.exhaustive-deps: Warns about missing dependencies inuseEffect,useCallback, etc. Start with"warn"to learn patterns without blocking builds.
Preset Configurations
For quick setup, use a recommended preset:
{
"extends": ["plugin:react-hooks/recommended"]
}
This applies sensible defaults for both rules without manual configuration.
How It Helps
The moment you write if (condition) { useState(...) }, your editor lights up with an error. No runtime bugs needed—instant feedback while coding.
Key Takeaway: Installing eslint-plugin-react-hooks and enabling its rules gives you real-time enforcement of Hook rules, catching violations during development instead of through runtime bugs.