mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Regression test: Missing unmount after re-order (#20285)
Adds a regression test for a bug I found in the effects refactor. The bug was that reordering a child that contains passive effects would cause the child to "forget" that it contains passive effects. This is because when a Placement effect is scheduled by the reconciler, it would override all of the fiber's flags, including its "static" ones: ``` child.flags = Placement; ``` The problem is that we use a static flag to use a "static" flag to track that a fiber contains passive effects. So what happens is that when the tree is deleted, the unmount effect is never fired. In the new implementation, the fix is to add the Placement flag without overriding the rest of the bitmask: ``` child.flags |= Placement; ``` (The old implementation doesn't need to be changed because it does not use static flags for this purpose.)
This commit is contained in:
@@ -3370,4 +3370,50 @@ describe('ReactHooksWithNoopRenderer', () => {
|
||||
});
|
||||
expect(Scheduler).toHaveYielded(['Unmount layout B', 'Unmount passive B']);
|
||||
});
|
||||
|
||||
it('regression: deleting a tree and unmounting its effects after a reorder', async () => {
|
||||
const root = ReactNoop.createRoot();
|
||||
|
||||
function Child({label}) {
|
||||
useEffect(() => {
|
||||
Scheduler.unstable_yieldValue('Mount ' + label);
|
||||
return () => {
|
||||
Scheduler.unstable_yieldValue('Unmount ' + label);
|
||||
};
|
||||
}, [label]);
|
||||
return label;
|
||||
}
|
||||
|
||||
await act(async () => {
|
||||
root.render(
|
||||
<>
|
||||
<Child key="A" label="A" />
|
||||
<Child key="B" label="B" />
|
||||
</>,
|
||||
);
|
||||
});
|
||||
expect(Scheduler).toHaveYielded(['Mount A', 'Mount B']);
|
||||
|
||||
await act(async () => {
|
||||
root.render(
|
||||
<>
|
||||
<Child key="B" label="B" />
|
||||
<Child key="A" label="A" />
|
||||
</>,
|
||||
);
|
||||
});
|
||||
expect(Scheduler).toHaveYielded([]);
|
||||
|
||||
await act(async () => {
|
||||
root.render(null);
|
||||
});
|
||||
|
||||
expect(Scheduler).toHaveYielded([
|
||||
'Unmount B',
|
||||
// In the regression, the reorder would cause Child A to "forget" that it
|
||||
// contains passive effects. Then when we deleted the tree, A's unmount
|
||||
// effect would not fire.
|
||||
'Unmount A',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user