最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nested TouchableOpacity Parent onPress not working - Stack Overflow

programmeradmin0浏览0评论

i had this issue where i'm trying to make sure the parent's onPress is triggered, but it wont

im trying to create a custom touchableOpacity ponent that able can be reusable, that wrap other Component so it can decide if the children can be shown or not and decide/alter what happen when the children ponent is pressed.

const CustomTouchable = (children, onPress) => {
   function handleOnPress = () => {
      if(validation){
         onPress();
      }
   }

   return <TouchableOpacity onPress={handleOnPress}>{children}</TouchableOpacity>
}


const MainComponent = () => {
   function onPress = () => {console.log('test')}

    <CustomTouchable onPress={onPress}>
       <TouchableOpacity style={styles.button}>
          <Text>Press Here</Text>
       </TouchableOpacity>
    </CustomTouchable>
}

but the parent onPress is not triggered, how can i trigger it?

i had this issue where i'm trying to make sure the parent's onPress is triggered, but it wont

im trying to create a custom touchableOpacity ponent that able can be reusable, that wrap other Component so it can decide if the children can be shown or not and decide/alter what happen when the children ponent is pressed.

const CustomTouchable = (children, onPress) => {
   function handleOnPress = () => {
      if(validation){
         onPress();
      }
   }

   return <TouchableOpacity onPress={handleOnPress}>{children}</TouchableOpacity>
}


const MainComponent = () => {
   function onPress = () => {console.log('test')}

    <CustomTouchable onPress={onPress}>
       <TouchableOpacity style={styles.button}>
          <Text>Press Here</Text>
       </TouchableOpacity>
    </CustomTouchable>
}

but the parent onPress is not triggered, how can i trigger it?

Share Improve this question edited Apr 13, 2020 at 13:51 Nathan Priyasadie asked Apr 13, 2020 at 12:57 Nathan PriyasadieNathan Priyasadie 1332 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

This is because the touch event is received by the children and not the parent. Assign following prop to your Child Component

pointerEvents={"none"}

Make the second TouchableOpacity disabled like this

<TouchableOpacity onPress={onPress}>
  <TouchableOpacity
    disabled
    style={styles.button}
  >
    <Text>Press Here</Text>
  </TouchableOpacity>
  </TouchableOpacity>
发布评论

评论列表(0)

  1. 暂无评论