Skip to content

Bug: The ESLint react-hooks/immutability rule warns against using a callback internally #35576

@4eb0da

Description

@4eb0da

eslint-plugin-react-hooks version: 7.0.1

Steps To Reproduce

const Test: FC = () => {
    const onMouseDown = useCallback(() => {
        // warns here about using onMouseDown
        window.removeEventListener('mousedown', onMouseDown);
    }, []);

    useEffect(() => {
        window.addEventListener('mousedown', onMouseDown);

        return () => {
            window.removeEventListener('mousedown', onMouseDown);
        };
    }, [onMouseDown]);

    return <div>Hello</div>;
};

The current behavior

`onMouseDown` is accessed before it is declared, which prevents the earlier access from updating when this value changes over time.

The expected behavior

No errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions