【发布时间】:2017-02-14 14:56:21
【问题描述】:
以下代码给了我这个错误:“无法读取未定义的属性'CityName'”。但是当我调试代码时,数据状态仅在第一次渲染时为空,并且在该数据从 API 接收到数据之后。有没有办法强制渲染忽略第一个空状态?
class profile extends Component {
constructor(props) {
super(props);
this.state = {
data :[],
};
}
componentWillMount() {
axios.get(BASE_URL + 'user/' + 1)
.then(response => this.setState({data: response.data.Result}))
.catch(error => console.log(error));
}
render() {
return (
<View>
<Text>{this.state.data.Profile.CityName}</Text>
</View>
);
}
}
【问题讨论】:
标签: reactjs react-native