-
Notifications
You must be signed in to change notification settings - Fork 49.8k
Add ref to Offscreen component #25254
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
Changes from 1 commit
a86f80d
d8611ac
a8f7a96
1608017
4320341
c7e962f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1121,7 +1121,7 @@ function commitLayoutEffectOnFiber( | |
| if (flags & Ref) { | ||
| safelyAttachRef(finishedWork, finishedWork.return); | ||
| } | ||
| } else if (finishedWork.pendingProps.mode !== undefined) { | ||
| } else { | ||
| safelyDetachRef(finishedWork, finishedWork.return); | ||
| } | ||
| } else { | ||
|
|
@@ -2148,6 +2148,8 @@ function commitDeletionEffectsOnFiber( | |
| offscreenSubtreeWasHidden = | ||
| prevOffscreenSubtreeWasHidden || deletedFiber.memoizedState !== null; | ||
|
|
||
| safelyDetachRef(deletedFiber, nearestMountedAncestor); | ||
|
|
||
| recursivelyTraverseDeletionEffects( | ||
| finishedRoot, | ||
| nearestMountedAncestor, | ||
|
|
@@ -2833,6 +2835,7 @@ export function disappearLayoutEffects(finishedWork: Fiber) { | |
| // Nested Offscreen tree is already hidden. Don't disappear | ||
| // its effects. | ||
| } else { | ||
| safelyDetachRef(finishedWork, finishedWork.return); | ||
|
||
| recursivelyTraverseDisappearLayoutEffects(finishedWork); | ||
| } | ||
| break; | ||
|
|
@@ -2974,6 +2977,8 @@ export function reappearLayoutEffects( | |
| finishedWork, | ||
| includeWorkInProgressEffects, | ||
| ); | ||
|
|
||
| safelyAttachRef(finishedWork, finishedWork.return); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1121,7 +1121,7 @@ function commitLayoutEffectOnFiber( | |
| if (flags & Ref) { | ||
| safelyAttachRef(finishedWork, finishedWork.return); | ||
| } | ||
| } else if (finishedWork.pendingProps.mode !== undefined) { | ||
| } else { | ||
| safelyDetachRef(finishedWork, finishedWork.return); | ||
| } | ||
| } else { | ||
|
|
@@ -2148,6 +2148,8 @@ function commitDeletionEffectsOnFiber( | |
| offscreenSubtreeWasHidden = | ||
| prevOffscreenSubtreeWasHidden || deletedFiber.memoizedState !== null; | ||
|
|
||
| safelyDetachRef(deletedFiber, nearestMountedAncestor); | ||
|
|
||
| recursivelyTraverseDeletionEffects( | ||
| finishedRoot, | ||
| nearestMountedAncestor, | ||
|
|
@@ -2833,6 +2835,7 @@ export function disappearLayoutEffects(finishedWork: Fiber) { | |
| // Nested Offscreen tree is already hidden. Don't disappear | ||
| // its effects. | ||
| } else { | ||
| safelyDetachRef(finishedWork, finishedWork.return); | ||
|
||
| recursivelyTraverseDisappearLayoutEffects(finishedWork); | ||
| } | ||
| break; | ||
|
|
@@ -2974,6 +2977,8 @@ export function reappearLayoutEffects( | |
| finishedWork, | ||
| includeWorkInProgressEffects, | ||
| ); | ||
|
|
||
| safelyAttachRef(finishedWork, finishedWork.return); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1307,4 +1307,84 @@ describe('ReactOffscreen', () => { | |||||
| expect(offscreenRef.current).not.toBeNull(); | ||||||
| }); | ||||||
| }); | ||||||
sammy-SC marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| // @gate enableOffscreen | ||||||
| it('should detach ref if Offscreen is unmounted', async () => { | ||||||
| let offscreenRef; | ||||||
|
|
||||||
| function App({showOffscreen}) { | ||||||
| offscreenRef = useRef(null); | ||||||
| return showOffscreen ? ( | ||||||
| <Offscreen | ||||||
| mode={'manual'} | ||||||
| ref={ref => { | ||||||
| offscreenRef.current = ref; | ||||||
| }}> | ||||||
| <div /> | ||||||
| </Offscreen> | ||||||
| ) : null; | ||||||
| } | ||||||
|
|
||||||
| const root = ReactNoop.createRoot(); | ||||||
|
|
||||||
| await act(async () => { | ||||||
| root.render(<App showOffscreen={true} />); | ||||||
| }); | ||||||
|
|
||||||
| expect(offscreenRef.current).not.toBeNull(); | ||||||
|
|
||||||
| await act(async () => { | ||||||
| root.render(<App showOffscreen={false} />); | ||||||
| }); | ||||||
|
|
||||||
| expect(offscreenRef.current).toBeNull(); | ||||||
|
|
||||||
| await act(async () => { | ||||||
| root.render(<App showOffscreen={true} />); | ||||||
| }); | ||||||
|
|
||||||
| expect(offscreenRef.current).not.toBeNull(); | ||||||
| }); | ||||||
|
|
||||||
| // @gate enableOffscreen | ||||||
| it('should detach ref if Offscreen is unmounted', async () => { | ||||||
|
||||||
| it('should detach ref if Offscreen is unmounted', async () => { | |
| it('should detach ref if Offscreen is hidden by parent', async () => { |
Outdated
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.
oops
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.
This also needs to be wrapped in a Ref effect. Otherwise you might forget to schedule a Ref effect in the begin phase, but it sometimes works anyway accidentally because you happen to have another effect flag scheduled during the same commit.
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.
Also I think this should be a regular
elseblock. Notelse if. Anything that's not manual should have its ref detached.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.
I don't follow this. What do you mean it needs to be wrapped in a Ref effect?