mirror of
https://github.com/ant-design/ant-design.git
synced 2025-01-18 06:03:38 +08:00
refactor: improve useCombinedRefs code (#27181)
* improve: useCombinedRefs code * change location
This commit is contained in:
parent
acc2e03ddf
commit
179ee276c9
19
components/_util/hooks/useCombinedRefs.tsx
Normal file
19
components/_util/hooks/useCombinedRefs.tsx
Normal file
@ -0,0 +1,19 @@
|
||||
import * as React from 'react';
|
||||
import { fillRef } from '../ref';
|
||||
|
||||
function useCombinedRefs<T>(
|
||||
...refs: Array<React.MutableRefObject<T> | ((instance: T) => void) | null>
|
||||
) {
|
||||
const targetRef = React.useRef();
|
||||
|
||||
React.useEffect(() => {
|
||||
refs.forEach(ref => {
|
||||
if (!ref) return;
|
||||
fillRef(ref, targetRef.current);
|
||||
});
|
||||
}, [refs]);
|
||||
|
||||
return targetRef;
|
||||
}
|
||||
|
||||
export default useCombinedRefs;
|
@ -1,25 +1,6 @@
|
||||
import * as React from 'react';
|
||||
import Tooltip, { TooltipProps } from '../tooltip';
|
||||
|
||||
function useCombinedRefs(
|
||||
...refs: Array<React.MutableRefObject<unknown> | ((instance: unknown) => void) | null>
|
||||
) {
|
||||
const targetRef = React.useRef();
|
||||
|
||||
React.useEffect(() => {
|
||||
refs.forEach(ref => {
|
||||
if (!ref) return;
|
||||
|
||||
if (typeof ref === 'function') {
|
||||
ref(targetRef.current);
|
||||
} else {
|
||||
ref.current = targetRef.current;
|
||||
}
|
||||
});
|
||||
}, [refs]);
|
||||
|
||||
return targetRef;
|
||||
}
|
||||
import useCombinedRefs from '../_util/hooks/useCombinedRefs';
|
||||
|
||||
const SliderTooltip = React.forwardRef<unknown, TooltipProps>((props, ref) => {
|
||||
const { visible } = props;
|
||||
|
Loading…
Reference in New Issue
Block a user