我是 React
和 React native
的新手,我正在尝试从 API 检索数据
然后呈现它,但我似乎遇到问题。
我可以从 API
获取数据,但是当我尝试渲染
时,我遇到了各种各样的错误。
基本上,我要做的就是渲染从 API 返回的照片。应该很简单吧?如果有人能指出我正确的方向,我将不胜感激。
我收到如下错误:
undefined is not an object (evaluating 'this.props.photos') in RenderPhotos_render
我可能太早进入了 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>
)
}
});
最佳答案
for those who dont find solution to their problems in answers above:-
这解决了我的问题: 我从
更改了代码import {React} from 'react';
到
import React from 'react';
发生的事情是,因为 React
默认从模块中导出,所以我不能用花括号将它包裹起来。
关于javascript - 渲染时获取 undefined 不是 React native 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34478162/