Skip to content

[Compiler Bug]: setter not correctly recognized as setter, required in useCallback dependency array #35118

@phryneas

Description

@phryneas

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/#N4Igzg9grgTgxgUxALhAHQHYEsC2AHCGAFwAIAqEgQzBICUFK5SAzGCHEtEGBprgbkwIAHgWIlmUDEywQMJKGAQBlKACMwcGFjxFZGABSYSJCLv00AvCWABfTAEobmYyThywpYK5NgA1joANK62JNZmeh6CGK7uGJ4kPFoMRAhhJAZOlgB8Gf46JAD8ChgAJgjMWBgIpSTINrYOLvIkADYIpADaEBoIMABulGrtgSRKRADyvQND7QC66fSMRAB0iipElKlGLSYRFiv5eEUkGFCtrXWJCMlbCK4O0bEepEk8d-TMi7yr658GbxSCEernWAElIDhCHgABZYOAAGUoAE9oEQAKLMZg3IgGTJhXLeXbXW6pT4rOCwHgYUjWQF3aImRpPFpYL4GACERxIADIeSQOT0lDNhsDnMTxlNhYNRXihX0Ze10vTtg41YySPYYi04gkeJ5KOJrEsmGslABhSgXNSMPx4rKEnwkNkZeUi9pOIkmb1jDpShWzBAAm7vMkVClUhA0zIg4lapmjbrTRUIOaxkwAegzJE6KzzCwAKjC0lVsTAeLVyngo+VpMiSAB3agkAAGkuTgZbozUUFIRGLY2g8DSVZrUbgWAQNAbfTSSelgbmKxIYIwZYrJFKbLLUdIo7K4-r-co8kgsEQK2aJh4RFg8hNvyUAFkEFD7QSMsBrgbxI1E-rNmINNoi1EBAhAOJKgAcxQEBcDEPtkWrGwSAABVaKAoKqCZzA8TUJDYDgAHIbTUBBWgAWjwDCsIwCj3iYCj3HwLB2hgDMt08IiWQML0SCzZi8FYrZ9CfCBynqLgrVaLhMFCMARLASopzQmjsNw+JHjA8AYQgBtV1SGAMCtMAUCIGAoAQWwgA

Repro steps

In the example,

  const restart = React.useCallback(() => {
    if (observable) {
      setObservable(recreateRef.current());
    }
  }, [observable]);

should be fine, but the compiler complains that setObservable is missing from the dependency array, even though it is aReact.useState return value from the same hook.

The inferred dependency was setObservable, but the source dependencies were [observable]. Inferred different dependency than source.

Apologies for the weird reproduction - I couldn't reproduce this from scratch, so it's a minimized variant of where we're seeing this in the wild: https://github.com/apollographql/apollo-client/blob/a43583377ccaae60b58bc45edfc88f32bed74a46/src/react/hooks/useSubscription.ts#L375

I know that on it's own, the reproduction doesn't make a lot of sense, but if I leave any of the leftover building blocks out, everything works nicely.

How often does this bug happen?

Every time

What version of React are you using?

19.1.0, but likely irrelevant - this is a library with peerDependency 17-19

What version of React Compiler are you using?

1.0.0/playground

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