diff --git a/src/ref.ts b/src/ref.ts index 9297a4be..fd9513b9 100644 --- a/src/ref.ts +++ b/src/ref.ts @@ -1,9 +1,11 @@ import type * as React from 'react'; -import { isValidElement } from 'react'; +import { isValidElement, version } from 'react'; import { ForwardRef, isMemo } from 'react-is'; import useMemo from './hooks/useMemo'; import isFragment from './React/isFragment'; +const ReactMajorVersion = Number(version.split('.')[0]); + export const fillRef = (ref: React.Ref, node: T) => { if (typeof ref === 'function') { ref(node); @@ -43,10 +45,7 @@ export const supportRef = (nodeOrComponent: any): boolean => { } // React 19 no need `forwardRef` anymore. So just pass if is a React element. - if ( - isReactElement(nodeOrComponent) && - (nodeOrComponent as any).props.propertyIsEnumerable('ref') - ) { + if (isReactElement(nodeOrComponent) && ReactMajorVersion >= 19) { return true; } diff --git a/tests/ref-19.test.tsx b/tests/ref-19.test.tsx index fab08af2..775fb317 100644 --- a/tests/ref-19.test.tsx +++ b/tests/ref-19.test.tsx @@ -1,6 +1,6 @@ /* eslint-disable no-eval */ import React from 'react'; -import { getNodeRef, useComposeRef } from '../src/ref'; +import { getNodeRef, useComposeRef, supportRef } from '../src/ref'; import { render } from '@testing-library/react'; jest.mock('react', () => { @@ -76,4 +76,20 @@ describe('ref: React 19', () => { expect(outerRef.current?.className).toBe('bamboo'); expect(container.querySelector('.test-output')?.textContent).toBe('bamboo'); }); + + it('supportRef with not provide ref', () => { + const Empty = () =>
; + + const Checker = ({ children }: { children: React.ReactElement }) => { + return

{String(supportRef(children))}

; + }; + + const { container } = render( + + + , + ); + + expect(container.querySelector('p')?.textContent).toBe('true'); + }); });