Skip to content

Commit 7ff1bd8

Browse files
Implement comprehensive React 19 compatibility fix for Storybook test runner
Co-authored-by: alichherawalla <[email protected]>
1 parent 4527ad3 commit 7ff1bd8

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

.storybook/preview.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
1-
import React from 'react';
21
import ReactDOM from 'react-dom';
2+
import { createRoot } from 'react-dom/client';
33
import enMessages from '../app/translations/en.json';
44

5-
// React 19 compatibility shim for unmountComponentAtNode
5+
// React 19 compatibility: Patch missing ReactDOM methods
66
if (!ReactDOM.unmountComponentAtNode) {
7-
ReactDOM.unmountComponentAtNode = (container) => {
8-
if (container._reactRootContainer) {
7+
ReactDOM.unmountComponentAtNode = function(container) {
8+
if (container && container._reactInternalInstance) {
9+
// For older React versions compatibility
10+
container._reactInternalInstance.unmount();
11+
delete container._reactInternalInstance;
12+
return true;
13+
}
14+
15+
if (container && container._reactRootContainer) {
916
container._reactRootContainer.unmount();
1017
delete container._reactRootContainer;
1118
return true;
1219
}
20+
21+
// Fallback: clean the container
22+
if (container) {
23+
while (container.firstChild) {
24+
container.removeChild(container.firstChild);
25+
}
26+
return true;
27+
}
28+
1329
return false;
1430
};
1531
}
1632

33+
if (!ReactDOM.render) {
34+
ReactDOM.render = function(element, container, callback) {
35+
let root = container._reactRootContainer;
36+
if (!root) {
37+
root = container._reactRootContainer = createRoot(container);
38+
}
39+
40+
root.render(element);
41+
42+
if (callback) {
43+
setTimeout(callback, 0);
44+
}
45+
};
46+
}
47+
1748
export const parameters = {
1849
actions: { argTypesRegex: '^on[A-Z].*' },
1950
controls: {

0 commit comments

Comments
 (0)