react-native - 反应原生。如何使父元素响应子元素调用的 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>
);
}
解决方案
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))/>
}
推荐阅读
- python - 文件浏览器 GUI 读取文件路径
- javascript - React Material ui:想要在所有其他字段的顶部显示所有搜索到的 li 项目
- html - 使用 Google 幻灯片和 iframe 向 Google 协作平台添加动画
- excel - 如何根据上面的单元格突出显示excel中的单元格?
- excel - 从另一个工作表中动态获取使用复杂公式的值
- excel - 如何获取一个月的第一个实例并添加新行(包括屏幕截图)
- python - 如何在 python 中更改折线图上的 x 轴?
- c++ - 缺少 .qmake.stash 文件
- android - AdMob 匹配率仍然很低,但政策警告已在 1 个多月前被移除
- node.js - 在多条路线中使用猫鼬和快递