erksch/react-native-wheely

scrollToIndex out of range error

Closed this issue ยท 4 comments

When scrolling very fast to the top of large lists (example settings: length of options array >= 100, deceleration_rate="normal"), the following error can occur on android phones (tested on different phones):

Fatal Exception: com.facebook.react.common.JavascriptException
Invariant Violation: scrollToIndex out of range: requested index -1 but minimum is 0

I am facing the same issue (also on iOS Simulator). I just need to "swipe up" a few times when I reached the first item, then the error occurs.

I was also able to reproduce this on iOS. I'll look at it this week.

The problem is here:

const handleMomentumScrollEnd = (
    event: NativeSyntheticEvent<NativeScrollEvent>,
  ) => {
    const offsetY = event.nativeEvent.contentOffset.y;
    let index = Math.floor(Math.floor(offsetY) / itemHeight);
    const last = Math.floor(offsetY % itemHeight);
    if (last > itemHeight / 2) index++;

    if (index !== selectedIndex) {
      onChange(index);
    }
  };

In some cases, especially on iOS event.nativeEvent.contentOffset.y is actually negative.
This is properly due to the list's bounciness.
The index might also overshoot in the other direction when scrolling down.

Released the fix in 0.6.0