微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

反应原生如何使父元素响应子元素调用的 onPress

如何解决反应原生如何使父元素响应子元素调用的 onPress

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

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

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

Here you can see the screenshot

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>
);

}

解决方法

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))/>
        }
,

你的问题仍然没有意义。 如果您希望它们都具有相同的作用,那么您不需要 2 个按钮。 如果您希望它们仅在按下 'B' 时触发,那么您可以使 'A' 只是一个没有 onPress 效果的普通视图。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。