react-native - react native 里listview组件 的clonewithrows参数不能访问外部变量?

查看:643
本文介绍了react-native - react native 里listview组件 的clonewithrows参数不能访问外部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

updata(){

        let newData;
        fetch('http://127.0.0.1:8080/news',{method:'post',headers:{}})
            .then((response) => response.json())
            .then((responseJson) => {
                newData=responseJson.greet;this.setState({refreshing:false});alert(newData)
            })
            .catch((error) => {
                console.error(error);
            });

        const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

        this.setState({

            dataSource:ds.cloneWithRows(['ff',newData])
        })
    }

这边最后一行dataSource:ds.cloneWithRows(['ff',newData]),显示newData是空的,但上面alert显示newData值时字符串‘hello’,这是为啥?

解决方案

let newData;
        fetch('http://127.0.0.1:8080/news',{method:'post',headers:{}})
            .then((response) => response.json())
            .then((responseJson) => {
                newData=responseJson.greet;
                this.setState({
                    dataSource:ds.cloneWithRows(['ff',newData]),
                    refreshing:false,
                });
            })
            .catch((error) => {
                console.error(error);
            
            this.setState({
                    refreshing:false,
                });
            });

setState放到回调里面去

这篇关于react-native - react native 里listview组件 的clonewithrows参数不能访问外部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆