Skip to content

Commit 892e850

Browse files
committed
Move useCombinedRefs hook to 'hooks' file and fix import
1 parent 62e9606 commit 892e850

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

src/commons/useCombinedRefs.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/hooks.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useEffect, useRef, DependencyList} from 'react';
1+
import React, {useEffect, useRef, DependencyList} from 'react';
22

33
/**
44
* This hook avoid calling useEffect on the initial value of his dependency array
@@ -14,3 +14,24 @@ export const useDidUpdate = (callback: () => void, dep: DependencyList) => {
1414
}
1515
}, dep);
1616
};
17+
18+
export const useCombinedRefs = (...refs: React.Ref<any>[]) => {
19+
const targetRef = React.useRef();
20+
21+
React.useEffect(() => {
22+
refs.forEach(ref => {
23+
if (!ref) {
24+
return;
25+
}
26+
27+
if (typeof ref === 'function') {
28+
ref(targetRef.current);
29+
} else {
30+
// @ts-expect-error
31+
ref.current = targetRef.current;
32+
}
33+
});
34+
}, [refs]);
35+
36+
return targetRef;
37+
};

src/infinite-list/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {ScrollViewProps} from 'react-native';
77
import {DataProvider, LayoutProvider, RecyclerListView, RecyclerListViewProps} from 'recyclerlistview';
88

99
import constants from '../commons/constants';
10-
import useCombinedRefs from '../commons/useCombinedRefs';
10+
import {useCombinedRefs} from '../hooks';
1111

1212
const dataProviderMaker = (items: string[]) => new DataProvider((item1, item2) => item1 !== item2).cloneWithRows(items);
1313

0 commit comments

Comments
 (0)