Skip to content

[Compiler Bug]: Expected memoization doesn't happen if multiple factors come into play #35613

@JonasDoe

Description

@JonasDoe

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://github.com/JonasDoe/FailingReactCompiler

Repro steps

When I combine optional chaining with a try/catch pattern, doSomething isn't stable anymore and the useEffect callback (hence: the logging) is invoked multiple times.

export const MinimalFailingCase: React.FC = () => {
  const [count, setCount] = useState(0);
  const { doSomething } = useMinimalFailing();

  useEffect(() => {
    console.log('[MinimalFailing] Effect ran - doSomething changed'); // this is logged on every button click
  }, [doSomething]);

  return (
      <p>Count: {count}</p>
      <button onClick={() => setCount(c => c + 1)}>Increment</button>
  );
};

const useClient= ()=> {
    const [client] = useState({ doThing: async () => 'result' });
    return client;
}

const useMinimalFailing =()=> {
    const client = useClient();
    const doSomething = async () => {
        try {
            // ❌ Optional chaining - compiler doesn't memoize
            const result = await client?.doThing();
            return { type: 'ok' as const, result };
        } catch {
            return { type: 'err' as const };
        }
    };
    return { doSomething };
}

How often does this bug happen?

Every time

What version of React are you using?

19.2.3

What version of React Compiler are you using?

babel-plugin-react-compiler@1.0.0, if you mean that

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions