erksch / react-native-wheely

An all JavaScript wheel picker for react-native without any native code.
413 stars 59 forks source link

scrollToIndex out of range error #36

Closed heidtjoh closed 1 year ago

heidtjoh commented 1 year ago

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

c-goettert commented 1 year ago

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.

erksch commented 1 year ago

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

erksch commented 1 year ago

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.

erksch commented 1 year ago

Released the fix in 0.6.0