Подтвердить что ты не робот

React Native, оценивающий 'dataSource.rowIdentities

Я делаю основной ListView с dataSource в React-Native, с данными, которые я получаю.

class Movies extends Component {
  render() {
    if (this.state.dataSource.getRowCount() === 0) {
      return (
        <View style={styles.container}>
          <Text>
            loading....
          </Text>
        </View>
      );
    }
    else {
      return (
        <View style={styles.container}>
          <ListView
            dataSource={this.state.dataSource}
            renderRow={this.renderRow.bind(this)}
          />
        </View>
      );
    }
  }

  constructor(props){
        super(props);
        this.state = {
          dataSource: new ListView.DataSource({
            rowHasChanged: (row1, row2) => row1 !== row2,
          }),
        };
  }

  componentWillMount() {
    this.fetchData();
  }

  fetchData(){
    fetch(HOT_MOVIES_URL)
      .then((response) => response.json())
      .then((responseData) => {
        if (responseData) {
          this.setState({
            dataSource: this.state.dataSource.cloneWithRows(responseData),
          });
        }
      })
      .done();
  }
}

пока я получаю

undefined не является объектом (оценка "dataSource.rowIdentities" )

Ошибка, попробовал много способов, также не работает, содержит question, любые идеи?

4b9b3361

Ответ 1

Попробуйте настроить ListView DataSource как переменную многократного использования, а затем повторно использовать ее, когда вам это нужно. Кроме того, вам может потребоваться установить dataSource как пустой массив, пока ваши данные не войдут. Попробуйте что-то вроде этого:

var ds = new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2 })

class Movies extends Component {
  render() {
    if (this.state.dataSource.getRowCount() === 0) {
      return (
        <View style={styles.container}>
          <Text>
            loading....
          </Text>
        </View>
      );
    }
    else {
      return (
        <View style={styles.container}>
          <ListView
            dataSource={this.state.dataSource}
            renderRow={this.renderRow.bind(this)}
          />
        </View>
      );
    }
  }
  constructor(props){
        super(props);
        this.state = {
          dataSource: ds.cloneWithRows([]),
        };
  }

  componentWillMount() {
    this.fetchData();
  }

  fetchData(){
    fetch(HOT_MOVIES_URL)
      .then((response) => response.json())
      .then((responseData) => {
        if (responseData) {
          this.setState({
            dataSource: ds.cloneWithRows(responseData),
          });
        }
      })
      .done();
  }
}