Im trying to implement this relatively popular bottom sheet in React Native by @gorhom.
My aim is to open the bottom sheet from another component. I’ve tried to follow this answer – here .
As far as i am aware, the forwarding of the ref seems correct. However, when i check the console log of the element being pressed – it is returned as ‘undefined’
Code below
BottomSheetModalComponent
export default function BottomSheetModalComponent({ref}) {
// ref
const bottomSheetModalRef = useRef<BottomSheetModal>(null);
// variables
const snapPoints = useMemo(() => ['25%', '50%'], []);
// callbacks
const handlePresentModalPress = useCallback(() => {
ref.current?.present();
}, []);
const handleSheetChanges = useCallback((index: number) => {
console.log('handleSheetChanges', index);
}, []);
// renders
return (
<BottomSheetModalProvider>
<View>
<BottomSheetModal
ref={ref}
snapPoints={snapPoints}
onChange={handleSheetChanges}
>
<View>
<Text>Awesome 🎉</Text>
</View>
</BottomSheetModal>
</View>
</BottomSheetModalProvider>
);
};
Location Component
export default function LocationBar() {
// Create Ref
const userBottomSheetRef = useRef<BottomSheetModal>(null);
// Pass ref into the bottom sheet component
<LocationBottomSheet ref={userBottomSheetRef} snapPoints={["30%"]}/>
return (
<>
<View style={{ flexDirection: "row" }}>
<View style={styles.locationBar}>
<Octicons style={styles.locationIcon} name="location" size={18} />
<TouchableOpacity onPress={() => {
// LINE BELOW RETURNS AS UNDEFINED
userBottomSheetRef.current?.present()
}}>
<Text style={{fontSize: 17, fontWeight: '600'}}>{userLocation}</Text>
</TouchableOpacity>
<Ionicons style={styles.chevronIcon} name="chevron-down" size={12} />
</View>
</View>
</>
);
}
Thanks in advance
2
Answers
Some things to note: I didn’t this test, so you may have to fix some things, but the way it goes is mentioned below. Secondly, bottom sheet needs to be rendered, so you need to place it in return.
You need to use Forward Ref with useImperativeHandle, like this
Then use it in your component like this
Don’t need another layer with useImperativeHandle. It works fine without it. Here is my example which I’ve tested.
When you call it, it is as follows:
For the BottomSheetBackdrop to close in Android (iOS works fine without it) just add the GestureHandlerRootView as per following example to your starting point file i.e. App.tsx / index.js