Skip to content

Commit 0a803f6

Browse files
authored
Update eslint-plugin-react-hooks config docs (#8030)
We're updating eslint-plugin-react-hooks with support for useEffectEvent usage and updated configuration options for applying the useEffectEvent and other effect rules to custom effect hooks. This adds the documentation for the config syntax and adds mention to the useEffectEvent reference page that the linter should be used to ensure proper usage of Effect Events.
1 parent c3d7560 commit 0a803f6

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/content/reference/eslint-plugin-react-hooks/lints/exhaustive-deps.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,21 @@ useEffect(() => {
140140

141141
## Options {/*options*/}
142142

143-
This rule accepts an options object:
143+
You can configure custom effect hooks using shared ESLint settings (available in `eslint-plugin-react-hooks` 6.1.0 and later):
144+
145+
```js
146+
{
147+
"settings": {
148+
"react-hooks": {
149+
"additionalEffectHooks": "(useMyEffect|useCustomEffect)"
150+
}
151+
}
152+
}
153+
```
154+
155+
- `additionalEffectHooks`: Regex pattern matching custom hooks that should be checked for exhaustive dependencies. This configuration is shared across all `react-hooks` rules.
156+
157+
For backward compatibility, this rule also accepts a rule-level option:
144158

145159
```js
146160
{
@@ -152,4 +166,4 @@ This rule accepts an options object:
152166
}
153167
```
154168

155-
- `additionalHooks`: Regex for hooks that should be checked for exhaustive dependencies
169+
- `additionalHooks`: Regex for hooks that should be checked for exhaustive dependencies. **Note:** If this rule-level option is specified, it takes precedence over the shared `settings` configuration.

src/content/reference/eslint-plugin-react-hooks/lints/rules-of-hooks.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,21 @@ const [permissions, setPermissions] = useState(
159159
userType === 'admin' ? adminPerms : userPerms
160160
);
161161
```
162+
163+
## Options {/*options*/}
164+
165+
You can configure custom effect hooks using shared ESLint settings (available in `eslint-plugin-react-hooks` 6.1.0 and later):
166+
167+
```js
168+
{
169+
"settings": {
170+
"react-hooks": {
171+
"additionalEffectHooks": "(useMyEffect|useCustomEffect)"
172+
}
173+
}
174+
}
175+
```
176+
177+
- `additionalEffectHooks`: Regex pattern matching custom hooks that should be treated as effects. This allows `useEffectEvent` and similar event functions to be called from your custom effect hooks.
178+
179+
This shared configuration is used by both `rules-of-hooks` and `exhaustive-deps` rules, ensuring consistent behavior across all hook-related linting.

src/content/reference/react/useEffectEvent.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Returns an Effect Event function. You can call this function inside `useEffect`,
5353

5454
#### Caveats {/*caveats*/}
5555

56-
- **Only call inside Effects:** Effect Events should only be called within Effects. Define them just before the Effect that uses them. Do not pass them to other components or hooks.
56+
- **Only call inside Effects:** Effect Events should only be called within Effects. Define them just before the Effect that uses them. Do not pass them to other components or hooks. The [`eslint-plugin-react-hooks`](/reference/eslint-plugin-react-hooks) linter (version 6.1.0 or higher) will enforce this restriction to prevent calling Effect Events in the wrong context.
5757
- **Not a dependency shortcut:** Do not use `useEffectEvent` to avoid specifying dependencies in your Effect's dependency array. This can hide bugs and make your code harder to understand. Prefer explicit dependencies or use refs to compare previous values if needed.
5858
- **Use for non-reactive logic:** Only use `useEffectEvent` to extract logic that does not depend on changing values.
5959

@@ -88,5 +88,7 @@ function Page({ url }) {
8888
}
8989
```
9090

91-
You can pass reactive values like `url` as arguments to the Effect Event. This lets you access the latest values without making your Effect re-run for every change.
91+
In this example, the Effect should re-run after a render when `url` changes (to log the new page visit), but it should **not** re-run when `numberOfItems` changes. By wrapping the logging logic in an Effect Event, `numberOfItems` becomes non-reactive. It's always read from the latest value without triggering the Effect.
92+
93+
You can pass reactive values like `url` as arguments to the Effect Event to keep them reactive while accessing the latest non-reactive values inside the event.
9294

0 commit comments

Comments
 (0)