Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/ref.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(ref: React.Ref<T>, node: T) => {
if (typeof ref === 'function') {
ref(node);
Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 17 additions & 1 deletion tests/ref-19.test.tsx
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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 = () => <div />;

const Checker = ({ children }: { children: React.ReactElement }) => {
return <p>{String(supportRef(children))}</p>;
};

const { container } = render(
<Checker>
<Empty />
</Checker>,
);

expect(container.querySelector('p')?.textContent).toBe('true');
});
});
Loading