【发布时间】:2018-03-29 05:17:15
【问题描述】:
我在带有 Expo 的 windows 上使用 react native,在我的 iphone 5 上使用 expo 应用程序。 我希望从 web 服务 api 获取数据,使用 React Native 我有这个错误:App.js: unexpected token(15:6) 我希望从 web 服务中获取响应数组
这是我的代码:
import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native'
export default class App extends Component {
fetch('https://httpbin.org/get').then(function (response) {
return response;
}).then(function (response) {
setTimeout(function () {
main.setState({
infoStatus: 'loaded'
});
}, 300);
return response.json();
}).then(function (data) {alert(data);
main.setState({
city: data.name,
country: data.sys.country,
temperature: data.main.temp,
humidity: data.main.humidity,
wind: data.wind.speed
});
}).catch(function () {
main.setState({
infoStatus: 'error'
});
});
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome
</Text>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
})
【问题讨论】:
-
类中不能有任意的javascript。你必须把你的代码放在一个方法developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript json reactjs api react-native