我正在初始化此userName字段...
constructor(props){
super(props);
this.state ={ isLoading: true}
this.state = {userName: null}
}
要从“ SELECT”中获取,该“ SELECT”仅从表中检索一行...
componentDidMount(){
return fetch('http://creat1vedesign.com/user.php')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
userName: responseJson.userName,
}, function(){
});
})
.catch((error) =>{
console.error(error);
});
}
检索到的行是...
[{"userName":"carolf","name":"Carol Faria","picture":"https:\/\/scontent.fcgh4-1.fna.fbcdn.net\/v\/t1.0-9\/40643115_10209672379005555_7946763802064715776_n.jpg?_nc_cat=104&_nc_ht=scontent.fcgh4-1.fna&oh=ed8b3219863e6704326e429bd2899e9f&oe=5C7BE0F1","hasTab":"true"}]
然后在这里使用...
render() {
return (
<View style={{ flex: 1 }}>
<View style={{backgroundColor: '#F3A024', alignItems: 'center', height: 380 }}>
<Text style={styles.textBorder}>{this.state.userName}</Text>
但是userName不会使用获取的数据进行更新(它保持为null)。我想念的是什么?
答案 0 :(得分:0)
您从后端收到一个数组。因此,您不能设置responseJson.userName
而是设置responseJson[0].userName
。
当然最好在PHP脚本中返回一个对象而不是数组。