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’,这是为啥?

阅读 7.1k
1 个回答
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放到回调里面去

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题