在我的React-native应用程序中,我有一个屏幕,当用户按下按钮提交答案时,我希望显示一个警告对话框,告诉他们答案是正确还是不正确。因此,我在onPress
按钮的TouchableHighlight
方法中调用了一个函数,并在该函数中指定了警报对话框。但是,这会导致警报对话框反复出现,因为该函数在每一帧中都被连续调用。我如何才能只调用一次该函数?
相关代码:
渲染功能
render: function() {
return (
<View style={styles.container}>
<Text style={styles.title}>{this.state.title}</Text>
<TouchableHighlight style = {styles.button}
onPress={this.onSubmitPressed()}
underlayColor='#99d9f4'>
<Text style = {styles.buttonText}>SUBMIT</Text>
</TouchableHighlight>
</View>
);
},
验证功能:
onSubmitPressed: function() {
if (this.checkSolution) {
Alert.alert(
'Alert title',
"Correct!"
);
}
else {
Alert.alert(
'Alert title',
"Incorrect!"
);
}
},
最佳答案
您正在渲染函数中调用onSubmitPressed。将onPress={this.onSubmitPressed()}
更改为onPress={this.onSubmitPressed}
(注意缺少的括号)。
关于ios - 在React Native函数中创建Alert对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37231441/