Skip to content

Commit a9c9f99

Browse files
committed
fix: Component also need support ref check
1 parent 39b6bb0 commit a9c9f99

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/ref.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@ export function composeRef<T>(...refs: React.Ref<T>[]): React.Ref<T> {
2020
};
2121
}
2222

23-
export function supportRef(node: any): boolean {
23+
export function supportRef(nodeOrComponent: any): boolean {
24+
// Function component node
25+
if (
26+
nodeOrComponent.type &&
27+
nodeOrComponent.type.prototype &&
28+
!nodeOrComponent.type.prototype.render
29+
) {
30+
return false;
31+
}
32+
2433
// Function component
25-
if (node.type && node.type.prototype && !node.type.prototype.render) {
34+
if (typeof nodeOrComponent === 'function' && !nodeOrComponent.prototype.render) {
2635
return false;
2736
}
2837

tests/ref.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('ref', () => {
2222
<FC />
2323
</div>,
2424
);
25+
expect(supportRef(FC)).toBeFalsy();
2526
expect(supportRef(wrapper.props().children)).toBeFalsy();
2627
});
2728

@@ -32,6 +33,7 @@ describe('ref', () => {
3233
<FRC />
3334
</div>,
3435
);
36+
expect(supportRef(FRC)).toBeTruthy();
3537
expect(supportRef(wrapper.props().children)).toBeTruthy();
3638
});
3739

@@ -48,6 +50,7 @@ describe('ref', () => {
4850
<CC />
4951
</div>,
5052
);
53+
expect(supportRef(CC)).toBeTruthy();
5154
expect(supportRef(wrapper.props().children)).toBeTruthy();
5255
});
5356
});

0 commit comments

Comments
 (0)