javascript - 渲染时获取 undefined 不是 React native 中的对象

标签 javascript reactjs react-native

我是 ReactReact 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/

相关文章:

javascript - node js无法处理sns事件消息内容

javascript - AEM 6.x : How to access i18n translations via Javascript?

reactjs - 错误 TS2339 : Property 'for' does not exist on type 'HTMLProps<HTMLLabelElement>'

没有装饰器的reactjs mox无法工作

react-native - 如何发送 session 信息并在 expo react native 段和幅度集成中进行自动页面跟踪

javascript - 如何同步Web SQL数据库

javascript - Highcharts:带有垂直网格线的 xAxis

javascript - react native : ListView with section headers and collapsible-expandable items

javascript - 我的 javascript Async Await api 请求返回一个奇怪的对象

reactjs - React-navigation 用 React Context 包裹根 AppContainer