Skip to content

Commit da93cb5

Browse files
fix(app): hoaphantn7604#253 error handling included
Closes: hoaphantn7604#253
1 parent 7874a95 commit da93cb5

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/components/Dropdown/index.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import {
1818
Keyboard,
1919
KeyboardEvent,
2020
Modal,
21+
StatusBar,
2122
StyleSheet,
2223
Text,
2324
TouchableHighlight,
2425
TouchableWithoutFeedback,
2526
View,
2627
ViewStyle,
27-
StatusBar,
2828
} from 'react-native';
2929
import { useDetectDevice } from '../../toolkits';
3030
import { useDeviceOrientation } from '../../useDeviceOrientation';
@@ -265,24 +265,31 @@ const DropdownComponent: <T>(
265265
}, [value, data, getValue]);
266266

267267
const scrollIndex = useCallback(() => {
268-
if (autoScroll && data.length > 0 && listData.length === data.length) {
268+
if (autoScroll && data.length > 0 && listData?.length === data?.length) {
269269
setTimeout(() => {
270270
if (refList && refList?.current) {
271271
const defaultValue =
272272
typeof value === 'object' ? _.get(value, valueField) : value;
273273

274-
const index = _.findIndex(listData, (e: any) =>
274+
const index = _.findIndex(listData, (e) =>
275275
_.isEqual(defaultValue, _.get(e, valueField))
276276
);
277-
if (
278-
listData.length > 0 &&
279-
index > -1 &&
280-
index <= listData.length - 1
281-
) {
282-
refList?.current?.scrollToIndex({
283-
index: index,
284-
animated: false,
285-
});
277+
278+
if (index > -1 && index < listData.length) {
279+
try {
280+
refList.current.scrollToIndex({
281+
index: index,
282+
animated: false,
283+
});
284+
} catch (error) {
285+
console.warn(`scrollToIndex error: ${error}`);
286+
}
287+
} else {
288+
console.warn(
289+
`scrollToIndex out of range: requested index ${index} is out of 0 to ${
290+
listData.length - 1
291+
}`
292+
);
286293
}
287294
}
288295
}, 200);

0 commit comments

Comments
 (0)