Skip to content
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

Do not unmount layout effects on initial Offscreen mount #25592

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2880,7 +2880,7 @@ function commitMutationEffectsOnFiber(
}

if (isHidden) {
if (!wasHidden) {
if (current !== null && current.memoizedState === null) {
Copy link

@philIip philIip Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so my understanding is that the issue is happening bc when the hidden offscreen component is mounted, we are not running layout effects.

conceptually, why would we want to run layout effects if the component is hidden? what is the example of breaking behavior?

also it is kinda confusing so i think we should rename this or add a comment explaining what the actual behavior is (i.e. neverVisible) or sth like that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so my understanding is that the issue is happening bc when the hidden offscreen component is mounted, we are not running layout effects.

The issue is that layout effects are cleaned up even when Offscreen is hidden. If you look at the test, it may happen when two OffScreens are nested (keep in mind every Suspense boundary uses Offscreen).

if ((offscreenBoundary.mode & ConcurrentMode) !== NoMode) {
// Disappear the layout effects of all the children
recursivelyTraverseDisappearLayoutEffects(offscreenBoundary);
Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberCommitWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -2880,7 +2880,7 @@ function commitMutationEffectsOnFiber(
}

if (isHidden) {
if (!wasHidden) {
if (current !== null && current.memoizedState === null) {
sammy-SC marked this conversation as resolved.
Show resolved Hide resolved
sammy-SC marked this conversation as resolved.
Show resolved Hide resolved
if ((offscreenBoundary.mode & ConcurrentMode) !== NoMode) {
// Disappear the layout effects of all the children
recursivelyTraverseDisappearLayoutEffects(offscreenBoundary);
Expand Down
38 changes: 38 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactOffscreen-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,44 @@ describe('ReactOffscreen', () => {
expect(root).toMatchRenderedOutput(<span prop="Child" />);
});

// @gate enableOffscreen
it('nested offscreen does not call componentWillUnmount when hidden', async () => {
// This is a bug that appeared during production test of <unstable_Offscreen />.
// It is a very specific scenario with nested Offscreens. The inner offscreen
// goes from visible to hidden in synchronous update.
class ClassComponent extends React.Component {
render() {
return <Text text="Child" />;
}

componentWillUnmount() {
Scheduler.unstable_yieldValue('componentWillUnmount');
}
}

function App() {
const [isVisible, setIsVisible] = React.useState(true);

if (isVisible === true) {
setIsVisible(false);
}

return (
<Offscreen mode="hidden">
<Offscreen mode={isVisible ? 'visible' : 'hidden'}>
<ClassComponent />
</Offscreen>
</Offscreen>
);
}

const root = ReactNoop.createRoot();
await act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded(['Child']);
});

// @gate enableOffscreen
it('mounts/unmounts layout effects when visibility changes (starting hidden)', async () => {
function Child({text}) {
Expand Down