react-native-element-dropdown icon indicating copy to clipboard operation
react-native-element-dropdown copied to clipboard

Error: "Invariant Violation: scrollToIndex out of range: requested index 228 is out of 0 to 148"

Open jonqwerty opened this issue 1 year ago • 13 comments

Hi, I use react-native-element-dropdown. Data for displaying in dropdown counts about 300 items. From time to time I receive error : "Invariant Violation: scrollToIndex out of range: requested index 228 is out of 0 to 148" when I use search.

Here is code ` <View style={styles.container}>

  {renderLabel()}
  <Dropdown
    dropdownPosition={'bottom'}
    // flatListProps={}
    selectedTextProps={{
      numberOfLines: 1,
    }}
    style={[styles.dropdown]}
    containerStyle={styles.listOfitems}
    placeholderStyle={styles.placeholderStyle}
    selectedTextStyle={styles.selectedTextStyle}
    // iconStyle={styles.iconStyle}
    data={L}
    autoScroll
    labelField="label"
    valueField="value"
    placeholder={!isFocus ? t('Choose a branch') : '...'}
    value={choosenBranch}
    onFocus={() => setIsFocus(true)}
    onBlur={() => setIsFocus(false)}
    onChange={async item => {
      setChoosenBranch(item.value);
      setIsFocus(false);
    }}
    renderItem={item => renderItem(item)}
    search
    // searchField="value"
    searchPlaceholder={t('Search...')}
    inputSearchStyle={styles.inputSearchStyle}
    maxHeight={350}
  />
</View>`

"react-native": "0.70.5", "react-native-element-dropdown": "^2.3.0",

Screenshot_5

Is there any way to fix this?

jonqwerty avatar Mar 16 '23 16:03 jonqwerty

@jonqwerty Have you been able to solve this? Facing same issue

HERYORDEJY avatar Mar 19 '23 11:03 HERYORDEJY

@HERYORDEJY-DEV

I didn't solve this problem, but I avoided it. As I understand it, this error occurs when autoscrolling to the selected item and when the length of the list is more than 50 items (determined experimentally).

How to avoid it: autoScroll={false}. But then autoscroll will not be working :)

jonqwerty avatar Mar 19 '23 13:03 jonqwerty

hi @jonqwerty @HERYORDEJY-DEV , This error happen in version 2.8.1?

hoaphantn7604 avatar Mar 19 '23 13:03 hoaphantn7604

@hoaphantn7604 No, this error happen in version 2.3.0 "react-native-element-dropdown": "^2.3.0"

jonqwerty avatar Mar 19 '23 16:03 jonqwerty

@hoaphantn7604 No, this error happen in version 2.3.0 "react-native-element-dropdown": "^2.3.0" Did you try again on version 2.8.1?

hoaphantn7604 avatar Mar 21 '23 15:03 hoaphantn7604

@hoaphantn7604

I try version 2.8.1 and receive the same error: Screenshot_7 Screenshot_8 Screenshot_11

jonqwerty avatar Mar 21 '23 20:03 jonqwerty

I have the same issue, on the 2.9.0 version.

The other workaround would be to provide getItemLayout in flatListProps:

flatListProps={{
  getItemLayout: (data, index) => ({
    length: itemHeight,
    offset: itemHeight * index,
    index,
  }),
}}

wilkuintheair avatar Mar 31 '23 09:03 wilkuintheair

@wilkuintheair can you help me fix this error. i didn't find flatListProps

vinhvuongthe avatar Apr 22 '23 14:04 vinhvuongthe

Same error happening with [email protected] getting this error please help.

gani419 avatar Apr 26 '23 09:04 gani419

@HERYORDEJY-DEV

I didn't solve this problem, but I avoided it. As I understand it, this error occurs when autoscrolling to the selected item and when the length of the list is more than 50 items (determined experimentally).

How to avoid it: autoScroll={false}. But then autoscroll will not be working :)

Did you find any other way to fix this code even i am getting the same error

chinmay4github1987 avatar Apr 26 '23 12:04 chinmay4github1987

I placed the problem code in try catch.it stops the crash ===>

try{ refList?.current?.scrollToIndex({ index: index, animated: false, }); } catch(e){ console.log("error===>",e) }

in scrollIndex function

dogra-deepak-tftus avatar May 05 '23 09:05 dogra-deepak-tftus

The solution that worked for me is

add this prop in your dropdown, where itemHeight = 50.

This hack is surely gonna work.

flatListProps={{ getItemLayout: (data, index) => ({ length: itemHeight / data?.length, offset: itemHeight * (index), index, }), }}

Also apply add height to your dropdown style. I have added style = {{ height: 55 }}

rituoctifi avatar Sep 05 '23 10:09 rituoctifi

add autoScroll={false}

ThanSau1105 avatar Feb 02 '24 09:02 ThanSau1105