问题描述
我希望一个组件应该接受父级和子级的onpress事件,如下所示:
import { TouchableOpacity } from "react-native";
function MyComponent(){
testFunc1 = ()=>{
alert("test");
}
testFunc2 = ()=>{
alert("test");
}
return(
<TouchableOpacity onPress={this.testFunc1}>
<Button onPress={this.testFunc2} title="Call Test" />
</TouchableOpacity>
);
}
解决方法
您可以将点击动作道具传递给孩子
import { TouchableOpacity,Text} from "react-native";
Const childComponent=(props) =>{
Const handleAction={
props.clickAction();
alert("child action")
}
return(
<TouchableOpacity onPress={handleAction}>
<Text>touchableButton<Text>
</TouchableOpacity>
)
}