-
Notifications
You must be signed in to change notification settings - Fork 49.7k
Open
Labels
Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugA potential issue that we haven't yet confirmed as a bugType: Bug
Description
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-compilereslint-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
import * as React from "react";
export function Example({ things }) {
const thing = things.find(thing => thing.something > 0);
const [count, setCount] = React.useState(0);
const label = thing.label.toLowerCase();
const handleClick = React.useCallback(
() => {
setCount(count => count + 1);
},
[],
);
return <button onClick={handleClick}>{label}: {count}</button>;
};Repro steps
- Create a local state using
useState()hook - Place it between two lines of code that contain chained method calls
- Use state setter inside
useCallback()
Expected: state setter does not need to be mentioned in useCallback deps
Observed:
Found 1 error:
Compilation Skipped: Existing memoization could not be preserved
React Compiler has skipped optimizing this component because the existing manual memoization could
not be preserved. The inferred dependencies did not match the manually specified dependencies, which
could cause the value to change more or less frequently than expected. The inferred dependency was
`setCount`, but the source dependencies were []. Inferred dependency not present in source.
Interestingly, the bug only occurs if useState is 'sandwiched' between two other lines. These lines need to be related to each other and contain .method() calls. Just getting object properties (e.g. thing.label instead of thing.label.toLowerCase()) is not enough to trigger the bug.
How often does this bug happen?
Every time
What version of React are you using?
19.2.0+ (inside playground.react.dev)
What version of React Compiler are you using?
1.0.0+ (inside playground.react.dev)
Metadata
Metadata
Assignees
Labels
Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugA potential issue that we haven't yet confirmed as a bugType: Bug