Javascript “承诺然后回调上下文”;this.callFunc()不是函数;

Javascript “承诺然后回调上下文”;this.callFunc()不是函数;,javascript,react-native,Javascript,React Native,我试图在单击按钮时隐藏或显示React-native对话框,但我不知道为什么我的代码会生成以下错误: > this.callFunc() is not a function. 该指令在fetch.then回调中执行 我使用dialogVisible变量作为一个标志,以知道对话框应该隐藏还是显示 import React, { Component } from 'react'; import { ConfirmDialog } from 'react-native-simple-dialo

我试图在单击按钮时隐藏或显示React-native对话框,但我不知道为什么我的代码会生成以下错误:

> this.callFunc() is not a function.
该指令在fetch.then回调中执行

我使用
dialogVisible
变量作为一个标志,以知道对话框应该隐藏还是显示

import React, { Component } from 'react';
import { ConfirmDialog } from 'react-native-simple-dialogs';
export default class RegistrationScreen extends Component
{
    constructor(props)
    {
        super(props);
        this.state = {
            dialogVisible: false,
        };
        this.callFunc = this.callFunc.bind(this);
    }

    callFunc = () =>
    {
        if (this.state.dialogVisible) {
            this.setState({ dialogVisible: false });
        } else {
            this.setState({ dialogVisible: true });
        }
    };

    getRegData()
    {
        fetch('http://test/api/registration/', {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                Accept: 'application/json',
                Authorization: 'Bearer' + ' ' + token,
            },
        }).then(function (response) {
            if (response.status === 204) {
                this.callFunc(); // getting error here
            } else if (response.status === 200) {
                response
                    .json()
                    .then(function (object)
                    {

                    })
                    .catch(error =>
                    {
                        Alert.alert(error.message);
                    });
            }
        });
    }

    render()
    {
        let { voucherCode } = this.state;
        return (
            <View>
                <Text onPress={this.getRegData}>
                    click me
                </Text>
                <ConfirmDialog
                    visible={this.state.dialogVisible}
                    title="Error"
                    onTouchOutside={() => this.setState({ dialogVisible: false })}
                    positiveButton={{
                        title: 'OK',
                        onPress: () => alert('Ok touched!'),
                    }}>
                    <View>
                        <Image
                            style={{ height: 40, width: 40, alignSelf: 'center' }}
                            source={images.success_alert}
                        />
                    </View>
                </ConfirmDialog>
            </View>
        );
    }
}
import React,{Component}来自'React';
从“react native simple dialogs”导入{ConfirmDialog};
导出默认类注册屏幕扩展组件
{
建造师(道具)
{
超级(道具);
此.state={
dialogVisible:false,
};
this.callFunc=this.callFunc.bind(this);
}
callFunc=()=>
{
if(this.state.dialogVisible){
this.setState({dialogVisible:false});
}否则{
this.setState({dialogVisible:true});
}
};
getRegData()
{
取('http://test/api/registration/', {
方法:“GET”,
标题:{
“内容类型”:“应用程序/json”,
接受:'application/json',
授权:'承载'+''+令牌,
},
}).然后(功能(响应){
如果(response.status==204){
this.callFunc();//此处出现错误
}否则如果(response.status==200){
响应
.json()
.then(函数(对象)
{
})
.catch(错误=>
{
警报。警报(错误消息);
});
}
});
}
render()
{
设{voucherCode}=this.state;
返回(
点击我
this.setState({dialogVisible:false})
正按钮={{
标题:“好的”,
onPress:()=>警报('Ok toucted!'),
}}>
);
}
}

您正在使用
getRegData()
方法中的回调,该方法未绑定到组件实例。要么像
this.callFunc
一样绑定它,要么使用箭头函数形式:
getRegData=()=>{

谢谢你的回复,我正在做与你说的相同的事情,但是得到相同的错误。请你更改我上面的代码并回复。同时尝试使用箭头函数进行回调:
然后((回复)=>{
不工作!你能更改我上面的代码并回复吗