【发布时间】:2015-12-27 06:30:08
【问题描述】:
我是 React 和 React native 的新手,我正在尝试 retrieve data from an API 然后渲染它,但我似乎遇到了问题。
我可以从API 获取数据,但是当我尝试使用render 时,我会遇到各种错误。
基本上我要做的就是渲染从 API 返回的照片。应该很简单吧?任何能指出我正确方向的人都将不胜感激。
我收到如下错误:
未定义不是 RenderPhotos_render 中的对象(评估“this.props.photos”)
我可能太早跳入React Native...所以请原谅我缺乏知识!
var AwesomeProject = React.createClass({
getInitialState: function() {
return {
isLoading: true,
photos: this.props.photos
};
},
componentWillMount: function(){
fetch("http://localhost:3000/api/photos/", {
method: "GET",
headers: {
"x-access-token":"xxxxx",
"x-key":"xxxx"
},
})
.then((response) => response.json())
.then((responseData) => {
AlertIOS.alert(
"GET Response",
"Search Query -> " + JSON.stringify(responseData)
)
this.setState({
isLoading: false
});
this.setState({
photos: JSON.stringify(responseData)
});
})
.done();
},
render: function() {
if(this.state.isLoading){
return <View><Text>Loading...</Text></View>
}
return (
<RenderPhotos photos={this.props.photos}/>
);
},
});
var RenderPhotos = React.createClass({
getInitialState: function() {
return {
photos: this.props.photos
};
},
render: function(){
var photos = Photos;
return (
<View>
<Text> {this.props.photos[0]} </Text>
</View>
)
}
});
【问题讨论】:
标签: javascript reactjs react-native