react-native-bottom-sheet: [v4] | [v2] Unable to set initial index to be open

Bug

We’ve just upgraded the bottom-sheet version in our app from v4.4.7 to v4.6.0. On doing this we now get the following error anywhere we use the bottom sheet: 'index' was provided but out of the provided snap points range! expected value to be between -1, 0. We have four snap points passed into the drawer: -1 (hidden), 0 (closed), 1 (partially open), 2 (fully open). We want the drawer to start partially open, so we want index = 1 on the Bottom sheet component. However, it seems like we can only pass in index={-1} or index={0} otherwise we see this error.

Environment info

Library Version
@gorhom/bottom-sheet 4.6.0
react-native 0.72.6
react-native-reanimated 3.5.4
react-native-gesture-handler 2.12.0

Steps To Reproduce

  1. Implement a bottom sheet and pass in array of at least two snapPoints. Pass in an index of 1.

Describe what you expected to happen:

  1. The Bottom sheet starts at snapPoint 1 (height 600) on first load. Instead we get the error 'index' was provided but out of the provided snap points range! expected value to be between -1, 0.

Reproducible sample code

import React, { useMemo, useRef, useCallback } from 'react';
import { View, StyleSheet } from 'react-native';
import BottomSheet, { enableLogging } from '@gorhom/bottom-sheet';
import List from './components/List';
import SearchHandle from './components/SearchHandle';
import Button from './components/Button';

enableLogging();

const App = () => {
  // refs
  const bottomSheetRef = useRef(null);

  // variables
  const snapPoints = useMemo(() => [200, 400], []);

  // callbacks
  const handleExpandPress = useCallback(() => {
    bottomSheetRef.current?.expand();
  }, []);
  const handleCollapsePress = useCallback(() => {
    bottomSheetRef.current?.collapse();
  }, []);
  const handleClosePress = useCallback(() => {
    bottomSheetRef.current?.close();
  }, []);

  return (
    <View style={styles.container}>
      <Button onPress={handleExpandPress} title="Expand" />
      <Button onPress={handleCollapsePress} title="Collapse" />
      <Button onPress={handleClosePress} title="Close" />
      <BottomSheet
        ref={bottomSheetRef}
        snapPoints={snapPoints}
        keyboardBehavior="interactive"
        index={1}
        handleComponent={SearchHandle}
        animateOnMount={true}
        android_keyboardInputMode="adjustPan">
        <List type="FlatList" />
      </BottomSheet>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingVertical: 64,
    justifyContent: 'flex-start',
    backgroundColor: '#222',
  },
});

export default App;

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 24 (1 by maintainers)

Most upvoted comments

Another finding, is sending one value on the snapPoints array, this error happens, if sending more than one, this works taking latest value.

<BottomSheetModal ref={modalRef} index={1} snapPoints={['75%']}> //raise error

<BottomSheetModal ref={modalRef} index={1} snapPoints={['50%', '75%']}> //works opening straight to 75%

In case useful to anyone, I have a PR open here https://github.com/gorhom/react-native-bottom-sheet/pull/1694 that has a fix in, I’ve patched the package locally so I can use it.

const snapPoints = useMemo(() => ['70%', '93%'], [])
const { width, height } = Dimensions.get('window')

<BottomSheet
...
snapPoints={snapPoints}
containerHeight={height * 0.7} // use this
>
...

This will fix it.

Still experiencing this issue, but am using reanimated v3.