Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: eslint-plugin-react-hooks exhaustive-deps rule shouldn't warn when using an object's property #28316

Closed
julienw opened this issue Feb 13, 2024 · 3 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@julienw
Copy link

julienw commented Feb 13, 2024

eslint-plugin-react-hooks version: 4.6.0

Example code:

import { useCallback } from "react";
import { useFetcher } from "react-router-dom";

...
    const fetcher = useFetcher();
    const onClick = useCallback(() => fetcher.submit(XXX), [fetcher.submit]);
    //  warning  React Hook useCallback has a missing dependency: 'fetcher'. Either include it or remove the dependency array  react-hooks/exhaustive-deps

This is the recommended way of specifying the dependency for this method, see remix-run/react-router#10336.

Expected

There's no warning.

Thanks!

@julienw julienw added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Feb 13, 2024
@billyjanitsch
Copy link
Contributor

This is by design. fetcher.submit(...) passes fetcher to the method call as this, so the rule is correct to require fetcher as a dependency. You can destructure to get a function that can be used as a dependency:

const fetcher = useFetcher();
const {submit} = fetcher
const onClick = useCallback(() => submit(XXX), [submit]);

See #16265, as well as #20755 for a proposal to make this configurable.

@julienw
Copy link
Author

julienw commented Feb 14, 2024

Ah interesting ! Yes I see the point.
Indeed I didn't want to do what you suggest because I wasn't sure if a proper this was needed... but failed to make the connection 😂

I'm closing the issue then. Hopefully this could help other folks though.

@julienw julienw closed this as completed Feb 14, 2024
@julianhille
Copy link

whoever reaches here:
do not use fetcher as a dependecy the fetcher object is not stable and if you do something like


const fetcher = useFetcher();

useEffect(() => {...}, [fetcher])

that will cause the useEffect to be called everytime as fetcher changes everytime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

3 participants