在React Native中仅一次致电服务

时间:2019-11-21 07:35:30

标签: reactjs react-native axios expo

我正在componentWillMount()中调用获取服务。服务以JSON I的形式响应了我下面提到的对象。但是我需要将服务对象的值放入我的变量/对象中,所以当我从服务中获取数据时我做了一个循环,它工作正常,但问题是将数据传递到我的对象/变量中花费的时间太多。将服务数据传递到我的对象至少需要45到50秒。如果我删除了循环,那么服务会完美地响应我,但是我需要将服务的值放入对象中。请为此提供最佳解决方案。谢谢

服务JSON响应

{
    "Capital": 0,
    "Code": "BOL",
    "Code2": "",
    "Continent": "",
    "GNP": 0,
    "Name": "Bolivia",
}

我需要将代码放入关键变量,将名称放入标签变量

axios.get('http://apiurl.com/api/user/GetCountries')
            .then((response) => {
                for (var i = 0; i < response.data.length; i++) {
                    var joined = this.state.countriesModel.concat({ key: response.data[i].Code, label: response.data[i].Name });
                    this.setState({ countriesModel: joined });
                }
                this.hideLoader();
            }).catch((err) => {
                console.log(err)
            });

1 个答案:

答案 0 :(得分:0)

我使数组处于外部状态,并且运行良好。 <CheckBox Content="{Binding MoziNeve}" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}"/> 花费太多时间来设置数据。

setState