react-native-gesture-handle swipeable methods not accessible with functional components

1.5k Views Asked by At

The React-Native-Gesture-Handler docs show Swipeable methods implemented in JS classes and only being accessible by the "this" keyword e.g. this.close

Example from docs:

...
<RectButton style={styles.leftAction} onPress={this.close}>
...
 

How do I use the "this" keyword (or its alternative) in React functional components to access these methods?

1

There are 1 best solutions below

1
On

Use Ref in this case:

const YourComponent = () => {

    const swipeRef = React.useRef()

    const closeSwipable = () => {
        swipeRef?.current?.close()
    }

    return (
        <Swipeable ref={swipeRef}>
        </Swipeable>
    )
}