Skip to content

Commit a02489a

Browse files
committed
Ensures extra calculations only run for react-native for web.
1 parent 399e434 commit a02489a

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

src/WheelPicker.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ViewProps,
1111
FlatListProps,
1212
FlatList,
13+
Platform
1314
} from 'react-native';
1415
import styles from './WheelPicker.styles';
1516
import WheelPickerItem from './WheelPickerItem';
@@ -51,7 +52,6 @@ const WheelPicker: React.FC<Props> = ({
5152
const [scrollY] = useState(new Animated.Value(0));
5253
const flatList = useRef<any>(null)
5354

54-
const SCROLL_DID_STOP_TIMEOUT = 500
5555
const lastScrollTimestamp = useRef(new Date().getTime())
5656

5757
const containerHeight = (1 + visibleRest * 2) * itemHeight;
@@ -101,21 +101,27 @@ const WheelPicker: React.FC<Props> = ({
101101

102102

103103
useEffect(() => {
104-
const intervalID = setInterval(() => {
105-
const time = new Date().getTime()
106-
const difference = time - lastScrollTimestamp.current
107-
if (difference > SCROLL_DID_STOP_TIMEOUT) {
108-
flatList.current?.scrollToIndex({ index: scrollIndex, animated: true })
104+
if (Platform.OS === "web") {
105+
const SCROLL_COOLDOWN_MILISECONDS = 100
106+
const SCROLL_DID_STOP_TIMEOUT = 500
107+
const intervalID = setInterval(() => {
108+
const time = new Date().getTime()
109+
const difference = time - lastScrollTimestamp.current
110+
if (difference > SCROLL_DID_STOP_TIMEOUT) {
111+
flatList.current?.scrollToIndex({ index: scrollIndex, animated: true })
112+
}
113+
}, SCROLL_COOLDOWN_MILISECONDS)
114+
return () => {
115+
clearInterval(intervalID)
109116
}
110-
}, 100)
111-
return () => {
112-
clearInterval(intervalID)
113117
}
114118
}, [scrollIndex])
115119

116120
useEffect(() => {
117-
if (onChange) {
118-
onChange(scrollIndex)
121+
if (Platform.OS === "web") {
122+
if (onChange) {
123+
onChange(scrollIndex)
124+
}
119125
}
120126
}, [scrollIndex])
121127

@@ -127,10 +133,12 @@ const WheelPicker: React.FC<Props> = ({
127133
layoutMeasurement: { height: number, width: number },
128134
}
129135
}) => {
130-
const positionY = event?.nativeEvent?.contentOffset?.y ?? 0;
131-
const index = Math.round(positionY / itemHeight)
132-
setScrollIndex(index)
133-
lastScrollTimestamp.current = new Date().getTime()
136+
if (Platform.OS === "web") {
137+
const positionY = event?.nativeEvent?.contentOffset?.y ?? 0;
138+
const index = Math.round(positionY / itemHeight)
139+
setScrollIndex(index)
140+
lastScrollTimestamp.current = new Date().getTime()
141+
}
134142
}
135143

136144
return (

0 commit comments

Comments
 (0)