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

Bugfix: LegacyHidden shouldn't defer effects #25442

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 37 additions & 2 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
enableUseEventHook,
enableStrictEffects,
enableFloat,
enableLegacyHidden,
} from 'shared/ReactFeatureFlags';
import {
FunctionComponent,
Expand Down Expand Up @@ -3419,7 +3420,23 @@ function commitPassiveMountOnFiber(
}
break;
}
case LegacyHiddenComponent:
case LegacyHiddenComponent: {
if (enableLegacyHidden) {
recursivelyTraversePassiveMountEffects(
finishedRoot,
finishedWork,
committedLanes,
committedTransitions,
);

if (flags & Passive) {
const current = finishedWork.alternate;
const instance: OffscreenInstance = finishedWork.stateNode;
commitOffscreenPassiveMountEffects(current, finishedWork, instance);
}
}
break;
}
case OffscreenComponent: {
// TODO: Pass `current` as argument to this function
const instance: OffscreenInstance = finishedWork.stateNode;
Expand Down Expand Up @@ -3600,7 +3617,25 @@ export function reconnectPassiveEffects(
// case HostRoot: {
// ...
// }
case LegacyHiddenComponent:
case LegacyHiddenComponent: {
if (enableLegacyHidden) {
recursivelyTraverseReconnectPassiveEffects(
finishedRoot,
finishedWork,
committedLanes,
committedTransitions,
includeWorkInProgressEffects,
);

if (includeWorkInProgressEffects && flags & Passive) {
// TODO: Pass `current` as argument to this function
const current: Fiber | null = finishedWork.alternate;
const instance: OffscreenInstance = finishedWork.stateNode;
commitOffscreenPassiveMountEffects(current, finishedWork, instance);
}
}
break;
}
case OffscreenComponent: {
const instance: OffscreenInstance = finishedWork.stateNode;
const nextState: OffscreenState | null = finishedWork.memoizedState;
Expand Down
39 changes: 37 additions & 2 deletions packages/react-reconciler/src/ReactFiberCommitWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
enableUseEventHook,
enableStrictEffects,
enableFloat,
enableLegacyHidden,
} from 'shared/ReactFeatureFlags';
import {
FunctionComponent,
Expand Down Expand Up @@ -3419,7 +3420,23 @@ function commitPassiveMountOnFiber(
}
break;
}
case LegacyHiddenComponent:
case LegacyHiddenComponent: {
if (enableLegacyHidden) {
recursivelyTraversePassiveMountEffects(
finishedRoot,
finishedWork,
committedLanes,
committedTransitions,
);

if (flags & Passive) {
const current = finishedWork.alternate;
const instance: OffscreenInstance = finishedWork.stateNode;
commitOffscreenPassiveMountEffects(current, finishedWork, instance);
}
}
break;
}
case OffscreenComponent: {
// TODO: Pass `current` as argument to this function
const instance: OffscreenInstance = finishedWork.stateNode;
Expand Down Expand Up @@ -3600,7 +3617,25 @@ export function reconnectPassiveEffects(
// case HostRoot: {
// ...
// }
case LegacyHiddenComponent:
case LegacyHiddenComponent: {
if (enableLegacyHidden) {
recursivelyTraverseReconnectPassiveEffects(
finishedRoot,
finishedWork,
committedLanes,
committedTransitions,
includeWorkInProgressEffects,
);

if (includeWorkInProgressEffects && flags & Passive) {
// TODO: Pass `current` as argument to this function
const current: Fiber | null = finishedWork.alternate;
const instance: OffscreenInstance = finishedWork.stateNode;
commitOffscreenPassiveMountEffects(current, finishedWork, instance);
}
}
break;
}
case OffscreenComponent: {
const instance: OffscreenInstance = finishedWork.stateNode;
const nextState: OffscreenState | null = finishedWork.memoizedState;
Expand Down
56 changes: 56 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactOffscreen-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,62 @@ describe('ReactOffscreen', () => {
]);
});

// @gate enableLegacyHidden
it('do not defer passive effects when prerendering a new LegacyHidden tree', async () => {
function Child({label}) {
useEffect(() => {
Scheduler.unstable_yieldValue('Mount ' + label);
return () => {
Scheduler.unstable_yieldValue('Unmount ' + label);
};
}, [label]);
return <Text text={label} />;
}

function App({showMore}) {
return (
<>
<Child label="Shell" />
<LegacyHidden
mode={showMore ? 'visible' : 'unstable-defer-without-hiding'}>
<Child label="More" />
</LegacyHidden>
</>
);
}

const root = ReactNoop.createRoot();

// Mount the app without showing the extra content
await act(async () => {
root.render(<App showMore={false} />);
});
expect(Scheduler).toHaveYielded([
// First mount the outer visible shell
'Shell',
'Mount Shell',

// Then prerender the hidden extra context. Unlike Offscreen, the passive
// effects in the hidden tree *should* fire
'More',
'Mount More',
]);

// The hidden content has been prerendered
expect(root).toMatchRenderedOutput(
<>
<span prop="Shell" />
<span prop="More" />
</>,
);

// Reveal the prerendered tree
await act(async () => {
root.render(<App showMore={true} />);
});
expect(Scheduler).toHaveYielded(['Shell', 'More']);
});

// @gate enableOffscreen
it('passive effects are connected and disconnected when the visibility changes', async () => {
function Child({step}) {
Expand Down