Skip to content

[react-hooks/exhaustive-deps] missed warning when passing a function #16573

@whatisaphone

Description

@whatisaphone

Do you want to request a feature or report a bug?

Arguably a bug in eslint-plugin-react-hooks

What is the current behavior?

The exhaustive-deps rule does not catch the following case:

const FooContext = React.createContext(() => {});

// Meanwhile, somewhere deep in the component tree
function Bar() {
  const foo = useContext(FooContext);
  useEffect(foo, []);
  return <div>A div walks into a Bar…</div>;
}

What is the expected behavior?

I think the plugin should suggest adding the dependency on foo:

useEffect(foo, [foo]);

It already makes this suggestion for the verbose form of the code:

// This code:
useEffect(() => foo(), []);
// is fixed to:
useEffect(() => foo(), [foo]);

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

  • eslint@6.2.2
  • eslint-plugin-react-hooks@2.0.1
  • react@16.9.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions