diff --git a/package.json b/package.json index 10359d3b..cd798ea4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-util", - "version": "5.44.3", + "version": "5.44.4", "description": "Common Utils For React Component", "keywords": [ "react", @@ -27,7 +27,7 @@ "coverage": "npm test -- --coverage", "lint": "eslint src/ --ext .tsx,.ts & eslint tests/ --ext .js", "prepare": "husky install", - "prepublishOnly": "npm run compile && np --yolo --no-publish", + "prepublishOnly": "npm run compile && np --yolo --no-publish --any-branch", "start": "dumi dev", "test": "rc-test" }, diff --git a/src/ref.ts b/src/ref.ts index a972dff3..54daede4 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); @@ -42,10 +44,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'); + }); });