未定义不是一个对象(评估'allRowsIDs.length')(React-Native)

问题描述:

我对React-Native很新。遵循基本的React-Native教程,我试图从“https://facebook.github.io/react-native/movies.json”中获取JSON数据。我可以显示titledescription性能,但是当我尝试使用ListView我收到以下错误显示“电影”属性:未定义不是一个对象(评估'allRowsIDs.length')(React-Native)

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'。你希望它指向你之前声明的数据源。

可以解决你的第一个问题,如果你改变: this.state = { dataSource: 'init', };

这样: this.state = { dataSource: ds };

然而,这只是打算让你一个新的错误消息。 Objects are not valid as a React child...的影响。你可以通过改变你的渲染函数来返回一个简单的字符串而不是整个对象。我会建议你从标题开始并从那里移动。试试这个,你应该对你的方式:

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

+0

非常感谢你,那工作! – rnboi05

+0

然后,您应该将其标记为正确;-) –