Skip to content

Commit

Permalink
fix: dedupe findDOMNode warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tmkx authored and yinkaihui committed Jul 2, 2024
1 parent e3ffaaf commit 65cb535
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion components/_util/react-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ if (isReact18 && createRoot) {
};
}

let warnedInstancesWeakSet: WeakSet<Function> | undefined;
function hasInstanceWarned(instance: ReactInstance) {
const ctor = instance.constructor;
if (typeof ctor !== 'function') return false;
if (!warnedInstancesWeakSet && typeof WeakSet === 'function') {
warnedInstancesWeakSet = new WeakSet();
}
const hasWarned = !!warnedInstancesWeakSet?.has(ctor);
warnedInstancesWeakSet?.add(ctor);
return hasWarned;
}

/**
*
* @param element
Expand Down Expand Up @@ -101,7 +113,7 @@ export const findDOMNode = (element: any, instance?: ReactInstance) => {
// 一般 useImperativeHandle 的元素拿到的 ref 不是 dom 元素且不存在 getRootDOMNode ,会走到这里。
if (instance) {
warning(
true,
!hasInstanceWarned(instance),
'Element does not define the `getRootDOMNode` method causing a call to React.findDOMNode. but findDOMNode is deprecated in StrictMode. Please check the code logic',
{ element, instance }
);
Expand Down

0 comments on commit 65cb535

Please sign in to comment.