undefined 不是一个对象(评估'allRowsIDs.length')(React-Native) [英] undefined is not an object (evaluating 'allRowsIDs.length') (React-Native)

查看:43
本文介绍了undefined 不是一个对象(评估'allRowsIDs.length')(React-Native)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 React-Native 很陌生.按照基本的 React-Native 教程,我尝试从https://获取 JSON 数据facebook.github.io/react-native/movies.json".我可以显示 titledescription 属性,但是当我尝试使用 ListView 显示电影"属性时,出现以下错误:

I'm very new to React-Native. Following the basic React-Native tutorial I'm trying to fetch JSON data from "https://facebook.github.io/react-native/movies.json". I can display the title and description properties but when I try to display the "movies" property using a ListView I get the following error:

undefined 不是一个对象(评估 'allRowsIDs.length')

undefined is not an object (evaluating 'allRowsIDs.length')

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  ListView
} from 'react-native';

var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

export default class AwesomeProject extends Component {
  constructor(props) {
    super(props);
    this.state = { 
        dataSource: 'init',
    };
  }

  componentWillMount() {
    fetch('https://facebook.github.io/react-native/movies.json')
      .then((response) => response.json())
      .then((responseJson) => {
        this.setState({ dataSource: ds.cloneWithRows(responseJson.movies) });
      })
      .catch((error) => {
        console.error(error);
      });
  }

  render() {
      return (
        <View style={styles.container}>
          <ListView
            dataSource={this.state.dataSource}
            renderRow={(rowData) => <Text>{rowData}</Text>}
          />
        </View>
      );
  }
}

推荐答案

您最初的问题是您已将 this.state.dataSource 设置为字符串 'init'.您希望它指向您之前声明的数据源.

Your initial problem is that you've set this.state.dataSource to the string 'init'. You want that to be pointing to the datasource that you declared earlier.

如果你改变,你可以解决你的第一个问题:

You can solve your first problem, if you change:

this.state = { 
   dataSource: 'init',
};

为此:

this.state = {
   dataSource: ds
};

但是,这只会让您看到一条新的错误消息.Objects is not valid as a React child... 的效果.您可以通过更改渲染函数以返回一个简单的字符串而不是整个对象来解决这个问题.我建议你从标题开始,然后从那里开始.试试这个,你应该就在路上:

However, that's just going to get you to a new error message. Something to the effect of Objects are not valid as a React child.... You can fix that by changing your render function to return a simple string rather than the whole object. I'll suggest you start with the title and move from there. Try this and you should be on your way:

 render() {
      return (
        <View style={styles.container}>
          <ListView
            dataSource={this.state.dataSource}
            renderRow={(rowData) => <Text>{rowData.title}</Text>}
          />
        </View>
      );
  }

这篇关于undefined 不是一个对象(评估'allRowsIDs.length')(React-Native)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆