Skip to content

Commit e98e41d

Browse files
[internal] Refactor useForkRef TS types: tighten cleanupRef and simplify ref typing (#46967)
Co-authored-by: Zeeshan Tamboli <[email protected]>
1 parent 02f0caa commit e98e41d

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

packages/mui-utils/src/useForkRef/useForkRef.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import * as React from 'react';
1919
export default function useForkRef<Instance>(
2020
...refs: Array<React.Ref<Instance> | undefined>
2121
): React.RefCallback<Instance> | null {
22-
const cleanupRef = React.useRef<void | (() => void)>(undefined);
22+
const cleanupRef = React.useRef<() => void>(undefined);
2323

24-
const refEffect = React.useCallback((instance: Instance | null) => {
24+
const refEffect = React.useCallback((instance: Instance) => {
2525
const cleanups = refs.map((ref) => {
2626
if (ref == null) {
2727
return null;
@@ -37,9 +37,9 @@ export default function useForkRef<Instance>(
3737
};
3838
}
3939

40-
(ref as React.RefObject<Instance | null>).current = instance;
40+
ref.current = instance;
4141
return () => {
42-
(ref as React.RefObject<Instance | null>).current = null;
42+
ref.current = null;
4343
};
4444
});
4545

@@ -57,11 +57,11 @@ export default function useForkRef<Instance>(
5757
return (value) => {
5858
if (cleanupRef.current) {
5959
cleanupRef.current();
60-
(cleanupRef as React.RefObject<void | (() => void)>).current = undefined;
60+
cleanupRef.current = undefined;
6161
}
6262

6363
if (value != null) {
64-
(cleanupRef as React.RefObject<void | (() => void)>).current = refEffect(value);
64+
cleanupRef.current = refEffect(value);
6565
}
6666
};
6767
// TODO: uncomment once we enable eslint-plugin-react-compiler // eslint-disable-next-line react-compiler/react-compiler -- intentionally ignoring that the dependency array must be an array literal

0 commit comments

Comments
 (0)