Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jonqwerty opened this issue Mar 16, 2023 · 13 comments

Comments

@jonqwerty
Copy link

jonqwerty commented Mar 16, 2023

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
`

  {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?

@HERYORDEJY
Copy link

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

@jonqwerty
Copy link
Author

jonqwerty commented Mar 19, 2023

@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 :)

@hoaphantn7604
Copy link
Owner

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

@jonqwerty
Copy link
Author

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

@hoaphantn7604
Copy link
Owner

@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?

@jonqwerty
Copy link
Author

@hoaphantn7604

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

@wilkuintheair
Copy link

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,
  }),
}}

@vinhvuongthe
Copy link

vinhvuongthe commented Apr 22, 2023

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

@gani419
Copy link

gani419 commented Apr 26, 2023

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

@chinmay4github1987
Copy link

@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

@dogra-deepak-tftus
Copy link

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

@rituoctifi
Copy link

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 }}

@ThanSau1105
Copy link

add autoScroll={false}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants