首页 > 解决方案 > 反应原生。如何使父元素响应子元素调用的 onPress

问题描述

我有两个用 TouchableOpacity 包裹的视图,一个在另一个。绿色的,我们称之为 A(父母),蓝色的 - B(孩子)。

问题:当我按下 B 时,是否可以同时使 TouchableOpacity 工作?

一点解释 - 在我的应用程序中,我有类似小模态视图的东西,当这个模态可见时,我想要以下行为 - 当我在模态外按下时,我希望这个模态关闭(就像按下 B 一样)和其他 touchableOpacity,它位于下面,也会触发(如 A)

在这里你可以看到截图

function Test() {
return (
    <TouchableOpacity onPress={() => console.log('green touch')}>
        <View style={{height: '100%', backgroundColor: 'green', justifyContent: 'center', alignItems: 'center'}}>
            <TouchableOpacity onPress={() => console.log('blue touch')}>
                <View style={{height: 200, width: 200, backgroundColor: 'blue'}}/>
            </TouchableOpacity>
        </View>
    </TouchableOpacity>
);

}

标签: react-nativetouchableopacity

解决方案


What I have understand from question. If any changes happen in child u want to pass the data or we can notify your parent. Right?

 * Create a function in your Parent.   

    const sendDataToParent = (value) => {  console.log("---value")};

* Pass it to ur child component.

<ChildComponent sendDataToParent={sendDataToParent}/>


*In your child component get it from props

  

      const Child =({sendDataToParent})=>{
        return <TouchableOpacity onPress=(sendDataToParent(pass your value))/>
        }

推荐阅读