react/compiler
mofeiZ eda778b8ae
[compiler] Fix false positive memo validation (alternative) (#34319)
Alternative to #34276

---
(Summary taken from @josephsavona 's #34276)
Partial fix for #34262. Consider this example:

```js
function useInputValue(input) {
  const object = React.useMemo(() => {
    const {value} = transform(input);
    return {value};
  }, [input]);
  return object;
}
```

React Compiler breaks this code into two reactive scopes:
* One for `transform(input)`
* One for `{value}`

When we run ValidatePreserveExistingMemo, we see that the scope for
`{value}` has the dependency `value`, whereas the original memoization
had the dependency `input`, and throw an error that the dependencies
didn't match.

In other words, we're flagging the fact that memoized _better than the
user_ as a problem. The more complete solution would be to validate that
there is a subgraph of reactive scopes with a single input and output
node, where the input node has the same dependencies as the original
useMemo, and the output has the same outputs. That is true in this case,
with the subgraph being the two consecutive scopes mentioned above.

But that's complicated. As a shortcut, this PR checks for any
dependencies that are defined after the start of the original useMemo.
If we find one, we know that it's a case where we were able to memoize
more precisely than the original, and we don't report an error on the
dependency. We still check that the original _output_ value is able to
be memoized, though. So if the scope of `object` were extended, eg with
a call to `mutate(object)`, then we'd still correctly report an error
that we couldn't preserve memoization.

Co-authored-by: Joe Savona <joesavona@fb.com>
2025-09-09 14:26:52 -04:00
..
apps/playground [playground] Fix CompilerError mismatch (#34420) 2025-09-08 15:06:54 -04:00
docs Update DEVELOPMENT_GUIDE.md (#32281) 2025-03-13 11:45:26 -04:00
fixtures [cleanup] Remove compiler runtime-compat fixture library (#31430) 2024-11-05 14:14:39 -05:00
packages [compiler] Fix false positive memo validation (alternative) (#34319) 2025-09-09 14:26:52 -04:00
scripts [compiler] Derive ErrorSeverity from ErrorCategory (#34401) 2025-09-06 12:41:29 -04:00
.eslintrc.js
.gitignore Remove leftover Rust script (#33314) 2025-05-20 12:20:51 -04:00
CHANGELOG.md [compiler] Update changelog for 19.1.0-rc.2 (#33207) 2025-05-15 10:34:11 -04:00
package.json [compiler] Script to produce markdown of lint rule docs (#34260) 2025-08-22 09:59:28 -07:00
README.md
yarn.lock [compiler] Aggregate error reporting, separate eslint rules (#34176) 2025-08-21 14:53:34 -07:00

React Compiler

React Compiler is a compiler that optimizes React applications, ensuring that only the minimal parts of components and hooks will re-render when state changes. The compiler also validates that components and hooks follow the Rules of React.

More information about the design and architecture of the compiler are covered in the Design Goals.

More information about developing the compiler itself is covered in the Development Guide.