Javascript React Native |无法读取setState的问题

Javascript React Native |无法读取setState的问题,javascript,android,api,react-native,Javascript,Android,Api,React Native,无法读取设置状态,print1的值保持不变。必须将其更改为数组[0]。日期,并且警报显示数组[0]。日期的值 问题是它以前是有效的 PS:没有显示错误 export default class WebServiceUse extends Component { constructor(props) { super(props); this.state = ({ print1: '0000', }) }

无法读取设置状态,print1的值保持不变。必须将其更改为数组[0]。日期,并且警报显示数组[0]。日期的值 问题是它以前是有效的

PS:没有显示错误

export default class WebServiceUse extends Component {
    constructor(props) {
        super(props);
        this.state = ({
            print1: '0000',
        })
    }
    componentDidMount() {
        fetch('https://**********', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ id: '1' })
        }).then((response) => response.json()).then((responseJson) => {
            let array = responseJson.ed5aaf3d933385698d872d0a0d5d4f36
            alert(array[0].date)
            this.setState = ({
                print1: array[0].date,
            })
        })
            .catch((error) => {
                console.error(error)
            });
    }
    render() {
        return (
            <View style={styles.container}>
                <Text>Test:</Text>
                <Text>{this.state.print1}</Text>
            </View>
        );
    }
}
试着这样做

export default class WebServiceUse extends Component {
    constructor(props) {
        super(props);
        this.state = ({
            print1: '0000',
        })
    }
    componentDidMount() {
        var that = this;
        fetch('https://**********', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ id: '1' })
        }).then((response) => response.json()).then((responseJson) => {
            let array = responseJson.ed5aaf3d933385698d872d0a0d5d4f36
            alert(array[0].date)
            that.setState({
                print1: array[0].date,
            })
        })
            .catch((error) => {
                console.error(error)
            });
    }
    render() {
        return (
            <View style={styles.container}>
                <Text>Test:</Text>
                <Text>{this.state.print1}</Text>
            </View>
        );
    }
}

当您在构造函数中设置状态时,this.state=正在分配state对象的初始值,因此它不是真正的函数。在生命周期的下一步,调用this.setState是一个将现有状态与更改合并的函数。所以改变

        this.state = ({
        print1: '0000',
    })

另外,您没有分配状态,您正在调用函数,因此不要使用=

应该是

         this.setState({
         print1: array[0].date
        })
this.setState{print1:array[0].date}。你写了一封额外的信=
         this.setState = ({
         print1: array[0].date,
        })
         this.setState({
         print1: array[0].date
        })