Skip to content

Commit 16a98af

Browse files
authored
Infra/use orientation hook in components (#1627)
* Create useOrientation hook * Update generated types * Use useOrientation hook in TabController * Fix lint errors
1 parent 6b0800d commit 16a98af

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

src/components/tabController/index.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// TODO: support commented props
2-
import React, {PropsWithChildren, useMemo, useEffect, useRef, useState, useCallback} from 'react';
2+
import React, {PropsWithChildren, useMemo, useEffect, useState, useCallback} from 'react';
33
import _ from 'lodash';
44
import {useAnimatedReaction, useSharedValue, withTiming, runOnJS} from 'react-native-reanimated';
55
import {Constants} from '../../helpers';
6-
import {orientations} from '../../helpers/Constants';
6+
import {useOrientation} from '../../hooks';
77
import {asBaseComponent} from '../../commons/new';
88
import {LogService} from '../../services';
99
import TabBarContext from './TabBarContext';
@@ -58,25 +58,16 @@ function TabController({
5858
children
5959
}: PropsWithChildren<TabControllerProps>) {
6060
const [screenWidth, setScreenWidth] = useState<number>(Constants.windowWidth);
61-
const orientation = useRef<orientations>(Constants.orientation);
62-
const dimensionsChangeListener = useRef<any>();
6361

6462
if (items?.length < 2) {
6563
console.error('TabController component expect a minimum of 2 items');
6664
}
6765

68-
useEffect(() => {
69-
const onOrientationChange = () => {
70-
if (orientation.current !== Constants.orientation) {
71-
orientation.current = Constants.orientation;
72-
setScreenWidth(Constants.windowWidth);
73-
}
74-
};
75-
dimensionsChangeListener.current = Constants.addDimensionsEventListener(onOrientationChange);
76-
return () => {
77-
Constants.removeDimensionsEventListener(dimensionsChangeListener.current || onOrientationChange);
78-
};
79-
}, []);
66+
useOrientation({
67+
onOrientationChange: () => {
68+
setScreenWidth(Constants.windowWidth);
69+
}
70+
});
8071

8172
const pageWidth = useMemo(() => {
8273
return carouselPageWidth || screenWidth;

0 commit comments

Comments
 (0)