Skip to content

Commit 7329717

Browse files
committed
feat: support react 19 #367
1 parent b70849f commit 7329717

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/utils/src/usePortal.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import React, { useState, useEffect, ReactPortal } from 'react';
2-
import { createPortal, unmountComponentAtNode } from 'react-dom';
1+
import React, { useState, useEffect, ReactPortal, useRef } from 'react';
2+
import { createPortal } from 'react-dom';
3+
import { createRoot, Root } from 'react-dom/client';
34

45
interface State {
56
render: (props: { children: React.ReactNode }) => ReactPortal | null;
67
remove: (elm?: HTMLElement) => void;
78
}
89

910
export const usePortal = () => {
11+
const ref = useRef<Root>();
1012
const [container] = React.useState<HTMLDivElement>(() => {
1113
const el = document.createElement('div');
14+
ref.current = createRoot(el);
1215
return el;
1316
});
1417
const [portal, setPortal] = useState<State>({
@@ -22,7 +25,7 @@ export const usePortal = () => {
2225
return createPortal(children, elmm);
2326
};
2427
const remove: State['remove'] = (elm) => {
25-
elm && unmountComponentAtNode(elm);
28+
elm && ref.current?.unmount();
2629
};
2730
return { render: Portal, remove };
2831
}, []);

0 commit comments

Comments
 (0)