user15573857
user15573857

Reputation:

How to let long press event bubble up in react native?

How do I make the long press event bubble up such that both triggered 1 and triggered 2 are printed?

  <Pressable style={styles.press2} onLongPress={() => { console.log('triggered 2') }}>
    <Text>Look Down</Text>
    <ScrollView>
      <Pressable style={{styles.press1}} onLongPress={() => { console.log('triggered 1') }}>
        <Text>click me</Text>
      </Pressable>
    </ScrollView>
  </Pressable>

I read this answer but I really don't understand it

Edit: is there any way to solve this without importing another package?

Upvotes: 1

Views: 1069

Answers (1)

Kipnoedels
Kipnoedels

Reputation: 1395

I don't think this is easily doable without a 'hacky' enable trick. I would personally use a LongPressGestureHandler from react-native-gesture-handler.

They have a simultaneousHandlers prop which makes multiple handlers work at the same time, so you could do something like this:

const childHandlerRef = useRef(null);

<LongPressGestureHandler simultaneousHandlers={childHandlerRef}>
  <View>
    <LongPressGestureHandler ref={childHandlerRef}>
      // other components
    </LongPressGestureHandler>
  </View>
</LongPressGestureHandler>

You can read more about it here.

Upvotes: 1

Related Questions