Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否在React本机函数中创建警报对话框?_Ios_Reactjs_Dialog_React Native_Alert - Fatal编程技术网

Ios 是否在React本机函数中创建警报对话框?

Ios 是否在React本机函数中创建警报对话框?,ios,reactjs,dialog,react-native,alert,Ios,Reactjs,Dialog,React Native,Alert,在我的React原生应用程序中,我有一个屏幕,当用户按下按钮提交答案时,我希望出现一个警告对话框,告诉他们答案是正确的还是错误的。因此,我在touchable highlight按钮的onPress方法中调用一个函数,并在该函数中指定警报对话框。但是,这会导致警报对话框一次又一次地重新出现,因为在每一帧中都会连续调用该函数。如何使函数只被调用一次 相关代码: 渲染功能 render: function() { return ( &l

在我的React原生应用程序中,我有一个屏幕,当用户按下按钮提交答案时,我希望出现一个警告对话框,告诉他们答案是正确的还是错误的。因此,我在
touchable highlight
按钮的
onPress
方法中调用一个函数,并在该函数中指定警报对话框。但是,这会导致警报对话框一次又一次地重新出现,因为在每一帧中都会连续调用该函数。如何使函数只被调用一次

相关代码: 渲染功能

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。将
onPress={this.onSubmitPressed()}
更改为
onPress={this.onSubmitPressed}
(注意缺少的括号)。

您正在渲染函数中调用onSubmitPressed。将
onPress={this.onSubmitPressed()}
更改为
onPress={this.onSubmitPressed}
(注意缺少的括号)。

成功了,谢谢!为什么删除括号会改变行为?有括号时,您会将调用结果传递给onPress,而不是函数本身。但是你真正想要的是传递函数。这就是没有括号的情况。就是这样,谢谢!为什么删除括号会改变行为?有括号时,您会将调用结果传递给onPress,而不是函数本身。但是你真正想要的是传递函数。这就是没有括号的情况。
onSubmitPressed: function() {
    if (this.checkSolution) {
        Alert.alert(
            'Alert title',
            "Correct!"
        );
    }
    else {
        Alert.alert(
            'Alert title',
            "Incorrect!"
        );
    }
},