我刚接触React
和React native
,正在try retrieve data from an API
,然后渲染它,但我似乎遇到了问题.
我可以从API
中获取数据,但是当我try render
时,我会得到各种各样的错误.
基本上,我要做的就是渲染从API返回的照片.应该很简单吧?如果有人能给我指出正确的方向,我将不胜感激.
我遇到了如下错误:
undefined不是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>
)
}
});