Skip to content

Commit

Permalink
[compiler] repro of false positive for ValidatePreserveManualMemo
Browse files Browse the repository at this point in the history
ghstack-source-id: 235c5bae0828b79db758871006e4c091923ca5fd
Pull Request resolved: #30431
  • Loading branch information
josephsavona committed Jul 24, 2024
1 parent f987f1a commit c08b516
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

## Input

```javascript
// @flow @validatePreserveExistingMemoizationGuarantees
import { identity } from "shared-runtime";

component Component(
disableLocalRef,
ref,
) {
const localRef = useFooRef();
const mergedRef = useMemo(() => {
return disableLocalRef ? ref : identity(ref, localRef);
}, [disableLocalRef, ref, localRef]);
return <div ref={mergedRef} />;
}

```


## Error

```
7 | ) {
8 | const localRef = useFooRef();
> 9 | const mergedRef = useMemo(() => {
| ^^^^^^^
> 10 | return disableLocalRef ? ref : identity(ref, localRef);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 11 | }, [disableLocalRef, ref, localRef]);
| ^^^^ CannotPreserveMemoization: React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This value may be mutated later, which could cause the value to change unexpectedly (9:11)
12 | return <div ref={mergedRef} />;
13 | }
14 |
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow @validatePreserveExistingMemoizationGuarantees
import { identity } from "shared-runtime";

component Component(
disableLocalRef,
ref,
) {
const localRef = useFooRef();
const mergedRef = useMemo(() => {
return disableLocalRef ? ref : identity(ref, localRef);
}, [disableLocalRef, ref, localRef]);
return <div ref={mergedRef} />;
}

0 comments on commit c08b516

Please sign in to comment.