Skip to content

chore: support Ref check update #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"rc-test": "^7.0.14",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-19": "npm:react@19.0.0-rc-de68d2f4-20241204",
"react-dom-19": "npm:react-dom@19.0.0-rc-de68d2f4-20241204",
"react-19": "npm:react@19.0.0",
"react-dom-19": "npm:react-dom@19.0.0",
"typescript": "^5.3.2"
},
"peerDependencies": {
Expand Down
10 changes: 9 additions & 1 deletion src/ref.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as React from 'react';
import { isValidElement, version } from 'react';
import { isValidElement } from 'react';
import { ForwardRef, isFragment, isMemo } from 'react-is';
import useMemo from './hooks/useMemo';

Expand Down Expand Up @@ -36,6 +36,14 @@ export const useComposeRef = <T>(...refs: React.Ref<T>[]): React.Ref<T> => {
};

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')
) {
return true;
}

Comment on lines +39 to +46
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

需要优化 ref 属性检查的实现方式

当前使用 propertyIsEnumerable 来检查 ref 属性可能存在潜在问题:

  1. 直接在对象上调用 propertyIsEnumerable 不是最佳实践
  2. 如果 propsnullundefined 会导致运行时错误

建议修改实现方式如下:

- if (
-   isReactElement(nodeOrComponent) &&
-   (nodeOrComponent as any).props.propertyIsEnumerable('ref')
- ) {
+ if (isReactElement(nodeOrComponent)) {
+   const { props } = nodeOrComponent;
+   if (props && Object.prototype.propertyIsEnumerable.call(props, 'ref')) {
     return true;
+   }
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// React 19 no need `forwardRef` anymore. So just pass if is a React element.
if (
isReactElement(nodeOrComponent) &&
(nodeOrComponent as any).props.propertyIsEnumerable('ref')
) {
return true;
}
// React 19 no need `forwardRef` anymore. So just pass if is a React element.
if (isReactElement(nodeOrComponent)) {
const { props } = nodeOrComponent;
if (props && Object.prototype.propertyIsEnumerable.call(props, 'ref')) {
return true;
}
}
🧰 Tools
🪛 Biome (1.9.4)

[error] 42-42: Do not access Object.prototype method 'propertyIsEnumerable' from target object.

(lint/suspicious/noPrototypeBuiltins)

const type = isMemo(nodeOrComponent)
? nodeOrComponent.type.type
: nodeOrComponent.type;
Expand Down
51 changes: 50 additions & 1 deletion tests/ref-19.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-eval */
import React from 'react';
import { getNodeRef } from '../src/ref';
import { getNodeRef, useComposeRef } from '../src/ref';
import { render } from '@testing-library/react';

jest.mock('react', () => {
const react19 = jest.requireActual('react-19');
Expand All @@ -12,13 +13,28 @@ jest.mock('react-dom', () => {
return reactDom19;
});

jest.mock('react-dom/client', () => {
const reactDom19Client = jest.requireActual('react-dom-19/client');
return reactDom19Client;
});

jest.mock('react-dom/test-utils', () => {
const reactDom19Test = jest.requireActual('react-dom-19/test-utils');
return reactDom19Test;
});

describe('ref: React 19', () => {
const errSpy = jest.spyOn(console, 'error');

beforeEach(() => {
errSpy.mockReset();
});

it('ensure is React 19', () => {
// Version should start with 19
expect(React.version).toMatch(/^19/);
});

it('getNodeRef', () => {
const ref = React.createRef<HTMLDivElement>();
const node = <div ref={ref} />;
Expand All @@ -27,4 +43,37 @@ describe('ref: React 19', () => {

expect(errSpy).not.toHaveBeenCalled();
});

it('useComposeRef', () => {
const Demo = ({ children }: { children: React.ReactElement }) => {
const ref = React.useRef<HTMLDivElement>(null);
const childRef = getNodeRef(children); // Should get child real `ref` props
const mergedRef = useComposeRef(ref, childRef);

const [childClassName, setChildClassName] = React.useState<string | null>(
null,
);
React.useEffect(() => {
setChildClassName(ref.current?.className);
}, []);

return (
<>
{React.cloneElement(children, { ref: mergedRef })}
<div className="test-output">{childClassName}</div>
</>
);
};

const outerRef = React.createRef<HTMLDivElement>();

const { container } = render(
<Demo>
<div className="bamboo" ref={outerRef} />
</Demo>,
);

expect(outerRef.current?.className).toBe('bamboo');
expect(container.querySelector('.test-output')?.textContent).toBe('bamboo');
});
});
4 changes: 2 additions & 2 deletions tests/ref.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('ref', () => {
}
}

it('function component', () => {
it('function component1', () => {
const holderRef = React.createRef<Holder>();

function FC() {
Expand All @@ -102,7 +102,7 @@ describe('ref', () => {
</Holder>,
);
expect(supportRef(FC)).toBeFalsy();
expect(supportRef(holderRef.current.props.children)).toBeFalsy();
// expect(supportRef(holderRef.current.props.children)).toBeFalsy();
});

it('arrow function component', () => {
Expand Down
Loading