Skip to content

Bug: set-state-in-use-effect false negative when there are multiple instances #35291

@roryabraham

Description

@roryabraham

This code correctly produces a lint error:

function MyComponent() {
    const [loading, setLoading] = useState(false);
    useEffect(() => {
        setLoading(true);  // ✅ ESLint correctly flags this (1 error)
    }, []);
    // commented out code...
    return null;
}

But this code does not:

function MyComponent() {
    const [loading, setLoading] = useState(false);
    useEffect(() => {
        setLoading(true);  // ❌ BUG: Error no longer reported (should still be flagged!)
    }, []);
    const [firstName, setFirstName] = useState<string>('');
    const [lastName, setLastName] = useState<string>('');
    const [fullName, setFullName] = useState<string>('');
    useEffect(() => {
        setFullName(`${firstName} ${lastName}`);  // ❌ BUG: Also not reported
    }, [firstName, lastName]);
    return null;
}

React version: ^19.2.1

Steps To Reproduce

Created a minimal reproduction repo: https://github.com/roryabraham/set-state-in-effect-bug

The current behavior

In the second code snippet provided, no lint errors are thrown.

The expected behavior

Lint errors should be thrown for both setState calls in useEffect callbacks.

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