gorhom / react-native-bottom-sheet

A performant interactive bottom sheet with fully configurable options 🚀
https://gorhom.dev/react-native-bottom-sheet/
MIT License
7.08k stars 777 forks source link

feat: add generic type for data modal #2029

Closed maximpostnikov closed 4 days ago

maximpostnikov commented 1 week ago

Motivation

This PR addresses an issue related to #2023.

It introduces the ability to explicitly define types for data passed through refs, ensuring strict typing for this data.

const alertModalPrimaryRef = useRef<BottomSheetModal<AlertModalContentProps>>(null);

const handleUpdatePress = () => {
  alertModalPrimaryRef.current?.present({
      type: 'success',
      test: true,  // Throws error because `test` isn't defined in `AlertModalContentProps`
    })
}

Additionally, it would be beneficial if the data passed through children could also be typed.

export type AlertModalContentProps = {
  type: 'success' | 'error'
}

...

<BottomSheetModal<AlertModalContentProps>
  name={name}
  ref={ref}>
  {({ data }) => (
    <BottomSheetView>
      {data && <AlertModalContent {...data} />}
    </BottomSheetView>
  )}
</BottomSheetModal>
gorhom commented 4 days ago

thanks @maximpostnikov for improving the types 🙌