在React Native中,TouchableOpacity组件的onPress事件是在用户点击组件时触发的。如果你想要点击外层TouchableOpacity时不触发内部TouchableOpacity的事件,你可以使用e.stopPropagation()方法来阻止事件冒泡。

以下是一个示例代码:

import React from 'react';
import { TouchableOpacity, Text } from 'react-native';

const OuterTouchableOpacity = () => {
  const handleOuterPress = () => {
    console.log('Outer TouchableOpacity Pressed');
  };

  const handleInnerPress = (e) => {
    e.stopPropagation();
    console.log('Inner TouchableOpacity Pressed');
  };

  return (
    <TouchableOpacity onPress={handleOuterPress}>
      <TouchableOpacity onPress={handleInnerPress}>
        <Text>Inner TouchableOpacity</Text>
      </TouchableOpacity>
    </TouchableOpacity>
  );
};

export default OuterTouchableOpacity;

在上面的代码中,当用户点击内部TouchableOpacity时,handleInnerPress函数会阻止事件冒泡,从而不会触发外部TouchableOpacity的handleOuterPress函数。


原文地址: https://www.cveoy.top/t/topic/icEF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录