Skip to content

[Compiler Bug]: useEffectEvent bypasses react-hooks/set-state-in-effect lint rule #35390

@zyzo

Description

@zyzo

What kind of issue is this?

  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-hooks (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://playground.react.dev/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEwBUYCAogGaUJx4A0JZVNd5AbggHYNMIDKeAIZ4EBAL4FKMCBgIByGAiF15AbgA6XLQgAeOfATgQuYQgGFZOLt0IBeAgAoAlATsA+IloJGTZggDajGR4giIIALpufGGijvLyzpraXD6kFNS0eI4ubp7A3j4EAPTFBBhCMADWCAAmBEJgBEoqeAC0ABYQEFVgxSFtZuFtaFxtCJl0BAgwMjCFPiGxCPGiZonJPuKMARFJWoXGpoQm-AihwqLR6SxZHLY5rh5eqUVLlyvya3gbheL7KTSzEm2VyzwKrx8pQIABtRoQsI0yGAFgRTudli5NhIAYUlHhYKkuFAYTDkuJkiB6CAjpQ0ABzFDobC4Qh4ACeWDExAACjCoPTRgB5LB4NB+CRSGRyeQAIyEsoQMLaWH5grGLTobWM2DQMJmxVqaHWyS0jghUOKOqwepE4q4AFkILUEMgCBoQEJSR6tJIwHawHSEE0+QLhaL7WAklTwF0AO4ASR4My4XrAKEoaYQ4iAA

Repro steps

Calling setState in useEffect is marked as an error per react-hooks/set-state-in-effect lint:

  const [, setState] = useState('');

  useEffect(() => {
    // marked as react-hooks/set-state-in-effect error
    setState('test');
  }, []);

However there is no lint error if the setState is wrapped in an useEffectEvent:

  const onSetState = useEffectEvent(() => {
    setState('test');
  });

  useEffect(() => {
    // lint passes
    onSetState();
  });

This provides an easy escape hatch for developers who want to bypass the lint rule.

How often does this bug happen?

Every time

What version of React are you using?

19.2

What version of React Compiler are you using?

No compiler needed. eslint-plugin-react-hooks is 7.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions