1
This commit is contained in:
+4
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Compatible with React 18 or 19 to check if node is a Fragment.
|
||||
*/
|
||||
export default function isFragment(object: any): boolean;
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
const REACT_ELEMENT_TYPE_18 = Symbol.for('react.element');
|
||||
const REACT_ELEMENT_TYPE_19 = Symbol.for('react.transitional.element');
|
||||
const REACT_FRAGMENT_TYPE = Symbol.for('react.fragment');
|
||||
|
||||
/**
|
||||
* Compatible with React 18 or 19 to check if node is a Fragment.
|
||||
*/
|
||||
export default function isFragment(object) {
|
||||
return (
|
||||
// Base object type
|
||||
object && typeof object === 'object' && (
|
||||
// React Element type
|
||||
object.$$typeof === REACT_ELEMENT_TYPE_18 || object.$$typeof === REACT_ELEMENT_TYPE_19) &&
|
||||
// React Fragment type
|
||||
object.type === REACT_FRAGMENT_TYPE
|
||||
);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import type * as React from 'react';
|
||||
import type { Root } from 'react-dom/client';
|
||||
declare const MARK = "__rc_react_root__";
|
||||
type ContainerType = (Element | DocumentFragment) & {
|
||||
[MARK]?: Root;
|
||||
};
|
||||
export declare function render(node: React.ReactElement, container: ContainerType): void;
|
||||
export declare function unmount(container: ContainerType): Promise<void>;
|
||||
export {};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
import { createRoot } from 'react-dom/client';
|
||||
const MARK = '__rc_react_root__';
|
||||
|
||||
// ========================== Render ==========================
|
||||
|
||||
export function render(node, container) {
|
||||
const root = container[MARK] || createRoot(container);
|
||||
root.render(node);
|
||||
container[MARK] = root;
|
||||
}
|
||||
|
||||
// ========================= Unmount ==========================
|
||||
export async function unmount(container) {
|
||||
// Delay to unmount to avoid React 18 sync warning
|
||||
return Promise.resolve().then(() => {
|
||||
container[MARK]?.unmount();
|
||||
delete container[MARK];
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user