问题描述
我有两个用 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
效果的普通视图。