在React Native函数中创建Alert对话框?

use*_*348 1 alert dialog ios reactjs react-native

在我的React-native应用程序中,我有一个屏幕,当用户按下按钮提交答案时,我想要一个警告对话框,告诉他们答案是正确还是不正确.所以,我onPressTouchableHighlight按钮的方法中调用一个函数,在该函数中我指定了警告对话框.但是,这会导致"警报"对话框一遍又一遍地重新出现,因为在每个帧中连续调用该函数.如何让函数只被调用一次?

相关代码:渲染功能

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>
    );
},
Run Code Online (Sandbox Code Playgroud)

验证功能:

onSubmitPressed: function() {
    if (this.checkSolution) {
        Alert.alert(
            'Alert title',
            "Correct!"
        );
    }
    else {
        Alert.alert(
            'Alert title',
            "Incorrect!"
        );
    }
},
Run Code Online (Sandbox Code Playgroud)

Dan*_*dow 5

您正在渲染函数中调用onSubmitPressed.更改onPress={this.onSubmitPressed()}onPress={this.onSubmitPressed}(注意失踪括号中).