In ESLint 10's flat config, how do you wire eslint-plugin-react-hooks' preset, and what changes if you choose `recommended-latest` instead of `recommended`?
answer
- the object, not a string
- configs.flat holds the flat presets
- registers itself as react-hooks
- top-level configs are eslintrc-shaped
- latest adds experimental compiler rules
basics
~10 sInstall eslint-plugin-react-hooks, 7.1.0 or later for ESLint 10, and add reactHooks.configs.flat.recommended to the config; recommended-latest is the same plus the plugin's bleeding-edge experimental React Compiler rules.
solid answer
~40 sInstall `eslint-plugin-react-hooks` as a dev dependency (7.1.0 is the release that added ESLint 10 support), import it, and add the object `reactHooks.configs.flat.recommended` to the array or to the `extends` of an object with `files` such as `["src/**/*.{js,jsx}"]`. That object registers the plugin as `react-hooks` and turns on `react-hooks/rules-of-hooks` as an error, `react-hooks/exhaustive-deps` as a warning, and the React Compiler-based rules the plugin recommends. Do not write `extends: ["react-hooks/recommended"]`: the plugin's top-level `configs.recommended` is its eslintrc-format preset, so `defineConfig()` rejects it as an eslintrc config. `recommended-latest`, at `reactHooks.configs.flat["recommended-latest"]`, keeps the same two core rules and adds the bleeding-edge experimental compiler rules; choose it when the team wants those diagnostics early and accepts that experimental rules can change.
go deeper
Recall the README path reactHooks.configs.flat.recommended, and that this preset object registers the react-hooks namespace by itself, so its rules are written react-hooks/rule-name.
Explain why the string react-hooks/recommended fails: the top-level preset is eslintrc-shaped, and defineConfig() only falls back to a flat/recommended key.
Choose between recommended and recommended-latest deliberately, scope the preset with files, and place hooks overrides after it with the right prefix.
Decide whether experimental compiler diagnostics belong in the gate now or in a trial preset, and own the plugin version that every app shares.
## Installing, and the ESLint 10 floor `eslint-plugin-react-hooks` is React's official ESLint plugin. Install it as a development dependency next to ESLint. For ESLint 10, use **7.1.0 or later**: its changelog lists "Add ESLint v10 support" in 7.1.0, and its `peerDependencies` range includes `^10.0.0`. ## Wiring the flat preset The plugin's README documents the flat presets under `configs.flat`: - `reactHooks.configs.flat.recommended` - `reactHooks.configs.flat["recommended-latest"]` Each is a ready-made **config object** that does two things: it registers the plugin under the namespace `react-hooks` (`plugins: { "react-hooks": plugin }`) and sets the plugin's rules. Because the preset carries its own registration, you do not need a separate `plugins` entry, although adding one with the same imported object is harmless. Put it inside an object with `files`, so it reaches only React sources: ```js import reactHooks from "eslint-plugin-react-hooks"; import { defineConfig } from "eslint/config"; export default defineConfig([ { files: ["src/**/*.{js,jsx}"], extends: [reactHooks.configs.flat.recommended], rules: { "react-hooks/exhaustive-deps": "error" }, }, ]); ``` The object's own `rules` come after the preset, so the override wins, and it uses the `react-hooks/` prefix because that is the namespace the preset registered. ## Why the string form fails With `@eslint/js` you can write `extends: ["js/recommended"]`. The same pattern fails here, and the reason is in how `defineConfig()` resolves strings: 1. `"react-hooks/recommended"` looks up `configs.recommended` on the registered plugin. 2. In this plugin, the top-level `configs.recommended` is the **eslintrc-format** preset: its `plugins` is an array of names, `["react-hooks"]`. 3. `defineConfig()` sees an eslintrc config and looks for a fallback key named exactly `flat/recommended`. 4. The plugin keeps its flat presets in a nested `configs.flat` object, not under that key, so there is no fallback. 5. `defineConfig()` throws: `Plugin config "recommended" in plugin "react-hooks" is an eslintrc config and cannot be used in this context.` Use the object path from the README instead. Passing the top-level `reactHooks.configs.recommended` object directly fails too, since ESLint 10 rejects a `plugins` array as eslintrc format. ## `recommended` against `recommended-latest` | | `recommended` | `recommended-latest` | |---|---|---| | `react-hooks/rules-of-hooks` | error | error | | `react-hooks/exhaustive-deps` | warn | warn | | React Compiler-based rules | the ones the plugin recommends | those plus bleeding-edge experimental rules | | Fits | most teams | teams wanting new compiler diagnostics early | Since plugin 7.0.0 these are the only two presets; 7.0.0 removed the older `flat/recommended` and `recommended-latest-legacy` names, so guides that use them predate it. What each hooks rule and compiler rule actually reports is a React question, separate from wiring them. ## Where the preset sits among other presets In a shared config that stacks several presets, the hooks preset is one more array entry, and the usual flat config rules apply: - Place it after general presets such as `js/recommended` and before the team's own overrides, so the team can adjust any `react-hooks/*` severity. - Keep it in an object with `files` that match React sources only. The preset itself sets no `files`, so at the top level it would reach every linted file, including scripts and extracted Markdown code blocks. - Register the plugin once. If the shared config already carries the preset, an app should configure `react-hooks/*` rules in a later object instead of adding the plugin again from its own install. - Switching between `recommended` and `recommended-latest` is a one-line change in that object, which makes it easy to trial the experimental rules in one app before the shared config adopts them. ## Upgrading an old setup - An `.eslintrc` with `"extends": ["plugin:react-hooks/recommended"]` stopped working when ESLint 10.0.0 removed eslintrc; replace it with the flat preset object. - If a shared config already includes the preset, do not add it again from another installed copy; one namespace may map to only one plugin object. - Override hooks rules after the preset, in the same object's `rules` or a later object, always with the `react-hooks/` prefix.
- How do you apply the hooks preset only to one app's sources in a monorepo config?Put it in the `extends` of an object whose `files` names that app, for example `files: ["apps/roster/src/**/*.{js,jsx}"]`. `defineConfig()` combines the preset with the object's `files`, so the hooks rules and the `react-hooks` registration reach only those files.
- A guide tells you to use `reactHooks.configs["flat/recommended"]`. Why is it `undefined`?That key belonged to the plugin before 7.0.0, which removed `flat/recommended` and `recommended-latest-legacy` and kept two presets. Current versions expose the flat presets as `reactHooks.configs.flat.recommended` and `reactHooks.configs.flat["recommended-latest"]`.
- Write extends: ["plugin:react-hooks/recommended"] in eslint.config.js.
- The string react-hooks/recommended works the same as the preset object.
- recommended-latest just raises exhaustive-deps from warn to error.
- Every plugin exposes its flat preset at configs.recommended.
- The flat preset needs a separate plugins entry before it will load.
Assumes ESLint 10 with eslint-plugin-react-hooks 7.1.0 or later, the first release supporting ESLint 10; the plugin's two presets, recommended and recommended-latest, date from its 7.0.0 release.