kxd-react-route-cache
Version:
change color from react-route-cache for my project
20 lines (19 loc) • 653 B
JavaScript
import { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
export const Component = ({ show, children, to, style }) => {
const [div] = useState(() => document.createElement('div'));
useEffect(() => {
// 清空原来的style内联样式
div.style.cssText = '';
Object.keys(style || {}).forEach((key) => {
div.style[key] = style[key];
});
}, [style]);
useEffect(() => {
var _a;
if (show) {
(_a = to.current) === null || _a === void 0 ? void 0 : _a.replaceChildren(div);
}
}, [show]);
return createPortal(children, div);
};