-
Notifications
You must be signed in to change notification settings - Fork 49k
[compiler] Patch for reactive refs in inferred effect dependencies #32991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ts__/fixtures/compiler/infer-effect-dependencies/reactive-ref-ternary.expect.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @inferEffectDependencies | ||
import {useRef, useEffect} from 'react'; | ||
import {print, mutate} from 'shared-runtime'; | ||
|
||
function Component({cond}) { | ||
const arr = useRef([]); | ||
const other = useRef([]); | ||
// Although arr and other are both stable, derived is not | ||
const derived = cond ? arr : other; | ||
useEffect(() => { | ||
mutate(derived.current); | ||
print(derived.current); | ||
}); | ||
return arr; | ||
} | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies | ||
import { useRef, useEffect } from "react"; | ||
import { print, mutate } from "shared-runtime"; | ||
|
||
function Component(t0) { | ||
const $ = _c(4); | ||
const { cond } = t0; | ||
let t1; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t1 = []; | ||
$[0] = t1; | ||
} else { | ||
t1 = $[0]; | ||
} | ||
const arr = useRef(t1); | ||
let t2; | ||
if ($[1] === Symbol.for("react.memo_cache_sentinel")) { | ||
t2 = []; | ||
$[1] = t2; | ||
} else { | ||
t2 = $[1]; | ||
} | ||
const other = useRef(t2); | ||
|
||
const derived = cond ? arr : other; | ||
let t3; | ||
if ($[2] !== derived) { | ||
t3 = () => { | ||
mutate(derived.current); | ||
print(derived.current); | ||
}; | ||
$[2] = derived; | ||
$[3] = t3; | ||
} else { | ||
t3 = $[3]; | ||
} | ||
useEffect(t3, [derived]); | ||
return arr; | ||
} | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: exception) Fixture not implemented |
15 changes: 15 additions & 0 deletions
15
...ompiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref-ternary.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// @inferEffectDependencies | ||
import {useRef, useEffect} from 'react'; | ||
import {print, mutate} from 'shared-runtime'; | ||
|
||
function Component({cond}) { | ||
const arr = useRef([]); | ||
const other = useRef([]); | ||
// Although arr and other are both stable, derived is not | ||
const derived = cond ? arr : other; | ||
useEffect(() => { | ||
mutate(derived.current); | ||
print(derived.current); | ||
}); | ||
return arr; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,10 +83,10 @@ export const FIXTURE_ENTRYPOINT = { | |
import { c as _c2 } from "react/compiler-runtime"; // @inlineJsxTransform | ||
|
||
function Parent(t0) { | ||
const $ = _c2(2); | ||
const $ = _c2(3); | ||
const { children, ref } = t0; | ||
let t1; | ||
if ($[0] !== children) { | ||
if ($[0] !== children || $[1] !== ref) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what we want -- note that ref is reactive as it comes from props
mofeiZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (DEV) { | ||
t1 = <div ref={ref}>{children}</div>; | ||
} else { | ||
|
@@ -99,9 +99,10 @@ function Parent(t0) { | |
}; | ||
} | ||
$[0] = children; | ||
$[1] = t1; | ||
$[1] = ref; | ||
$[2] = t1; | ||
} else { | ||
t1 = $[1]; | ||
t1 = $[2]; | ||
} | ||
return t1; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
derived
is now correctly added as a dependency. Prior to this PR, this deps array was empty (playground link)