import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Navigator,
TouchableOpacity,
Image,
TextInput,
TouchableHighlight,
Alert
} from 'react-native';
import Button from 'react-native-button'
import {Actions} from 'react-native-router-flux'
import Home from './Home'
export class Weight extends Component{
constructor(props) {
super(props);
this.state = {
data: '',
data1:'',
textinput:'',
entryDate: '',
systol:''
}
componentDidMount(){
this._onPressButtonGET();
}
_onPressButtonPOST(){
fetch("url", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"entryDate":"3/2/2017 2:00 AM",
"systol": "120",
"mobileType":"ANDROID",
"userName":"menutest"
})})
.then((response) => response.json())
.then((responseData) => {
Alert.alert(
"Blood pressure data",
"Blood pressure data - " + JSON.stringify(responseData)
)
})
.done();
}
_onPressButtonGET () {
fetch("url", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({"mobileType":"ANDROID","userName":"menutest"})})
.then((response) => response.json())
.then((responseData) => {
this.setState({ data: JSON.stringify(responseData) })
})
.done();
}
render(){
return(
<View>
<TouchableHighlight onPress={this._onPressButtonPOST}>
<Text>Add</Text>
</TouchableHighlight>
<TouchableHighlight onPress={this._onPressButtonGET.bind(this)}>
<Text>show</Text>
</TouchableHighlight>
<Text>{this.state.responseData.entryDate}</Text>
<Text>{this.state.responseData.systol}</Text>
</View>
);
}
}
module.exports = Weight;
在上面的代码中,我得到的不确定不是对象(评估'this.state.responsedata.entrydata')。我已经在构造函数中声明了进入日期,但仍会遇到相同的错误。上述代码中有什么问题?如何解决?
使用
<Text>{this.state.entryDate}</Text>
我认为您应该使用
<Text>{this.state.entryDate}</Text>
而不是
<Text>{this.state.responsedata.entryDate}</Text>
检查一下它是否会按照您的预期工作。祝你好运