[DevTools] Fix memory leak when unmounting hoistables (#35741)

This commit is contained in:
Sebastian "Sebbie" Silbermann
2026-02-10 13:09:23 +01:00
committed by GitHub
parent 49c3b270f9
commit f23aa1d9f5
2 changed files with 56 additions and 1 deletions

View File

@@ -3616,4 +3616,59 @@ describe('Store', () => {
<div> <div>
`); `);
}); });
// @reactVersion >= 19
it('cleans up host hoistables', async () => {
function Left() {
return (
<style href="test.css" precedence="medium">
{'* {color:black}'}
</style>
);
}
function Right() {
return (
<style href="test.css" precedence="medium">
{'* {color:black}'}
</style>
);
}
await actAsync(() => {
render(
<>
<Left />
<Right />
</>,
);
});
// Ensure we're still testing deduplicated hoistables.
expect(document.head.querySelectorAll('style')).toHaveLength(1);
expect(store).toMatchInlineSnapshot(`
[root]
<Left>
<Right>
`);
let style = document.head.querySelector('style');
let styleID = agent.getIDForHostInstance(style).id;
expect(store.containsElement(styleID)).toBe(true);
await actAsync(() => {
render(
<>
<Right />
</>,
);
});
expect(store).toMatchInlineSnapshot(`
[root]
<Right>
`);
style = document.head.querySelector('style');
styleID = agent.getIDForHostInstance(style).id;
expect(store.containsElement(styleID)).toBe(true);
});
}); });

View File

@@ -991,8 +991,8 @@ function releaseHostResource(
// eslint-disable-next-line no-for-of-loops/no-for-of-loops // eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const firstInstance of resourceInstances) { for (const firstInstance of resourceInstances) {
publicInstanceToDevToolsInstanceMap.set( publicInstanceToDevToolsInstanceMap.set(
publicInstance,
firstInstance, firstInstance,
nearestInstance,
); );
break; break;
} }