简体   繁体   English

如何让长按事件在 React Native 中冒泡?

[英]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?如何使长按事件冒泡以便同时打印触发 1 和触发 2?

  <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?编辑:有没有办法在不导入另一个 package 的情况下解决这个问题?

I don't think this is easily doable without a 'hacky' enable trick.如果没有“hacky”启用技巧,我认为这不容易实现。 I would personally use a LongPressGestureHandler from react-native-gesture-handler .我个人会使用来自react-native-gesture-handlerLongPressGestureHandler

They have a simultaneousHandlers prop which makes multiple handlers work at the same time, so you could do something like this:他们有一个simultaneousHandlers道具,可以让多个处理程序同时工作,所以你可以这样做:

const childHandlerRef = useRef(null);

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

You can read more about it here .您可以在此处阅读更多相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM