【发布时间】:2018-06-03 21:42:05
【问题描述】:
我的 React Native 应用程序中有两个函数,我需要 TouchableOpacity 在按下时调用这两个函数。为此,我尝试在 onPress 方法中简单地使用箭头函数,其中包含两个函数,但这不起作用。我认为这与范围有关,但我不确定。当仅将其单独传递给 onPress 方法时,这两个函数都可以正常工作。这是代码(为了便于阅读,我对其进行了很多修改)请帮忙。
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>
)
}
【问题讨论】:
-
@Simons0n 我已经尝试过了,但也没有运气。我相信我展示的代码实际上是遵循该问题中的第二个答案(使用箭头函数)。我认为我的代码可能不起作用,因为我的两个函数都是由父类传递的?
-
您缺少括号:
() => { this.sendData(); this.props.hide(); }。不多也不少。
标签: javascript reactjs react-native