Skip to content

Commit ba3d534

Browse files
authored
fix(useRTGTransitionProps): fix ref warning for react 18.3+ (#102)
1 parent f17bc10 commit ba3d534

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/useRTGTransitionProps.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
TransitionProps as RTGTransitionProps,
55
TransitionStatus,
66
} from 'react-transition-group/Transition';
7+
import { getReactVersion } from './utils';
78

89
export type TransitionProps = RTGTransitionProps & {
910
children:
@@ -32,10 +33,14 @@ export default function useRTGTransitionProps({
3233
children,
3334
...props
3435
}: TransitionProps) {
36+
const { major } = getReactVersion();
37+
const childRef =
38+
major >= 19 ? (children as any).props.ref : (children as any).ref;
39+
3540
const nodeRef = useRef<HTMLElement>(null);
3641
const mergedRef = useMergedRefs(
3742
nodeRef,
38-
typeof children === 'function' ? null : (children as any).ref,
43+
typeof children === 'function' ? null : childRef,
3944
);
4045

4146
const normalize =

src/utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
/* eslint-disable import/prefer-default-export */
1+
import * as React from 'react';
2+
23
export function isEscKey(e: KeyboardEvent) {
34
return e.code === 'Escape' || e.keyCode === 27;
45
}
6+
7+
export function getReactVersion() {
8+
const parts = React.version.split('.');
9+
return {
10+
major: +parts[0],
11+
minor: +parts[1],
12+
patch: +parts[2],
13+
};
14+
}

0 commit comments

Comments
 (0)