Skip to content

Feature request: react-hooks/exhaustive-deps option to require deps arg. #22604

@ryandagg

Description

@ryandagg

Please add an option to the configuration for the react-hooks/exhaustive-deps eslint rule to require a deps argument for all hooks that can take it.

This new configuration option would throw a warning/error (don't care about default) for the code below:

const Demo = ({ thing }) => {
  useEffect(() => {
    console.log(thing)
  })
}

I am aware that this already throws an error:

const Demo = ({ thing }) => {
  useEffect(() => {
    console.log(thing)
  }, [])
}

This request is specifically concerning the case where no second argument is passed to hooks like useEffect, useCallback, useMemo, etc.

The configuration would be used in a way similar to this:
'react-hooks/exhaustive-deps': ['warn', {alwaysRequireDeps: true }]

Reasoning:
I can't think of a scenario where firing the callback of useEffect on every render is desired. An intentional eslint-disable-next-line is acceptable if this scenario arises.
A missing deps arg can cause sneaky bugs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    React 18Bug reports, questions, and general feedback about React 18Type: Discussion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions