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

Don't disappear layout effects unnecessarily #25660

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2880,12 +2880,13 @@ function commitMutationEffectsOnFiber(

if (isHidden) {
const isUpdate = current !== null;
const isAncestorOffscreenHidden = offscreenSubtreeIsHidden;
const wasHiddenByAncestorOffscreen =
offscreenSubtreeIsHidden || offscreenSubtreeWasHidden;
// Only trigger disapper layout effects if:
// - This is an update, not first mount.
// - This Offscreen was not hidden before.
// - No ancestor Offscreen is hidden.
if (isUpdate && !wasHidden && !isAncestorOffscreenHidden) {
// - Ancestor Offscreen was not hidden in previous commit.
if (isUpdate && !wasHidden && !wasHiddenByAncestorOffscreen) {
if ((finishedWork.mode & ConcurrentMode) !== NoMode) {
// Disappear the layout effects of all the children
recursivelyTraverseDisappearLayoutEffects(finishedWork);
Expand Down
7 changes: 4 additions & 3 deletions packages/react-reconciler/src/ReactFiberCommitWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -2880,12 +2880,13 @@ function commitMutationEffectsOnFiber(

if (isHidden) {
const isUpdate = current !== null;
const isAncestorOffscreenHidden = offscreenSubtreeIsHidden;
const wasHiddenByAncestorOffscreen =
offscreenSubtreeIsHidden || offscreenSubtreeWasHidden;
// Only trigger disapper layout effects if:
// - This is an update, not first mount.
// - This Offscreen was not hidden before.
// - No ancestor Offscreen is hidden.
if (isUpdate && !wasHidden && !isAncestorOffscreenHidden) {
// - Ancestor Offscreen was not hidden for previous pass.
if (isUpdate && !wasHidden && !wasHiddenByAncestorOffscreen) {
if ((finishedWork.mode & ConcurrentMode) !== NoMode) {
// Disappear the layout effects of all the children
recursivelyTraverseDisappearLayoutEffects(finishedWork);
Expand Down
92 changes: 67 additions & 25 deletions packages/react-reconciler/src/__tests__/ReactOffscreen-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ describe('ReactOffscreen', () => {
// goes from visible to hidden in synchronous update.
class ClassComponent extends React.Component {
render() {
return <Text text="Child" />;
return <Text text="child" />;
}

componentWillUnmount() {
Expand All @@ -287,47 +287,89 @@ describe('ReactOffscreen', () => {
}
}

let _setIsVisible;
let isFirstRender = true;
const root = ReactNoop.createRoot();
await act(async () => {
// Outer and inner offscreen are hidden.
root.render(
<Offscreen mode={'hidden'}>
<Offscreen mode={'hidden'}>
<ClassComponent />
</Offscreen>
</Offscreen>,
);
});

function App() {
const [isVisible, setIsVisible] = React.useState(true);
_setIsVisible = setIsVisible;
if (isFirstRender === true) {
setIsVisible(false);
isFirstRender = false;
}
expect(Scheduler).toHaveYielded(['child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="child" />);

return (
<Offscreen mode="hidden">
<Offscreen mode={isVisible ? 'visible' : 'hidden'}>
await act(async () => {
// Inner offscreen is visible.
root.render(
<Offscreen mode={'hidden'}>
<Offscreen mode={'visible'}>
<ClassComponent />
</Offscreen>
</Offscreen>
</Offscreen>,
);
}
});

expect(Scheduler).toHaveYielded(['child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="child" />);

const root = ReactNoop.createRoot();
await act(async () => {
root.render(<App />);
// Inner offscreen is hidden.
root.render(
<Offscreen mode={'hidden'}>
<Offscreen mode={'hidden'}>
<ClassComponent />
</Offscreen>
</Offscreen>,
);
});

expect(Scheduler).toHaveYielded(['Child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="Child" />);
expect(Scheduler).toHaveYielded(['child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="child" />);

await act(async () => {
_setIsVisible(true);
// Inner offscreen is visible.
root.render(
<Offscreen mode={'hidden'}>
<Offscreen mode={'visible'}>
<ClassComponent />
</Offscreen>
</Offscreen>,
);
});

expect(Scheduler).toHaveYielded(['Child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="Child" />);
Scheduler.unstable_clearYields();

await act(async () => {
// Outer offscreen is visible.
// Inner offscreen is hidden.
root.render(
<Offscreen mode={'visible'}>
<Offscreen mode={'hidden'}>
<ClassComponent />
</Offscreen>
</Offscreen>,
);
});

expect(Scheduler).toHaveYielded(['child']);

await act(async () => {
_setIsVisible(false);
// Outer offscreen is hidden.
// Inner offscreen is visible.
root.render(
<Offscreen mode={'hidden'}>
<Offscreen mode={'visible'}>
<ClassComponent />
</Offscreen>
</Offscreen>,
);
});

expect(Scheduler).toHaveYielded(['Child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="Child" />);
expect(Scheduler).toHaveYielded(['child']);
});

// @gate enableOffscreen
Expand Down