[英]React Native Touchable Opacity functions
I have two functions in my React Native app, and I need my TouchableOpacity to call both when pressed. 我的React Native应用程序中有两个函数,按下时需要我的TouchableOpacity来调用它们。 To do so I tried to simply use an arrow function in the onPress method with both functions inside, but this doesn't work. 为此,我尝试在onPress方法中简单地使用带有两个函数的箭头函数,但这是行不通的。 I assume it's something to do with scope, but I'm not sure. 我认为这与范围有关,但我不确定。 Both functions work correctly when simply passed into the onPress method alone. 仅将其单独传递给onPress方法时,这两个函数即可正常工作。 Here's the code (I've trimmed it a lot for readability) Please help. 这是代码(为了便于阅读,我对其进行了很多裁剪)请帮忙。
export class CreateItem extends React.Component {
constructor(props){
super(props);
}
sendData = () => {
itemData = this.state.item;
this.props.action(itemData); //the action function alters the parent state (this function works fine every other time)
}
render(){
return(
<TouchableOpacity
onPress={() => {
this.sendData;
this.props.hide; //This function is passed from the parent and works fine in other scenarios
}}
>
<Text>Add Item</Text>
</TouchableOpacity>
)
}
you missed the parentheses of functions 您错过了函数的括号
export class CreateItem extends React.Component {
constructor(props){
super(props);
}
sendData = () => {
itemData = this.state.item;
this.props.action(itemData); //the action function alters the parent state (this function works fine every other time)
}
render(){
return(
<TouchableOpacity
onPress={() => {
this.sendData();
this.props.hide(); //This function is passed from the parent and works fine in other scenarios
}}
>
<Text>Add Item</Text>
</TouchableOpacity>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.