Commit e3920259 authored by timel's avatar timel

fix: render way in react 18

parent a186cfa1
Pipeline #22885 failed with stages
in 13 minutes and 15 seconds
import { useEffect } from "react"; import { useEffect } from "react";
import { render, unmountComponentAtNode } from "react-dom"; import { createRoot } from "react-dom/client";
import styles from "./index.module.scss"; import styles from "./index.module.scss";
...@@ -26,12 +26,13 @@ const withPortalToBody = <P extends object>( ...@@ -26,12 +26,13 @@ const withPortalToBody = <P extends object>(
const newNode = document.createElement("div"); const newNode = document.createElement("div");
portalRoot.insertBefore(newNode, firstChild); portalRoot.insertBefore(newNode, firstChild);
const root = createRoot(newNode);
// 渲染组件到新的容器中 // 渲染组件到新的容器中
render(<WrappedComponent {...props} />, newNode); root.render(<WrappedComponent {...props} />);
return () => { return () => {
// 卸载组件并且移除容器 // 卸载组件并且移除容器
unmountComponentAtNode(newNode); setTimeout(() => root.unmount());
portalRoot.removeChild(newNode); portalRoot.removeChild(newNode);
}; };
}, []); }, []);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment