无法在React Native中将类作为函数错误调用

时间:2017-06-09 06:40:32

标签: android react-native react-native-android

我在React Native中创建一个ListView并获得"无法将类调用为函数" 错误。我也尝试添加" new"课前的关键字,但仍然没有解决错误。以下是我的代码:

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

    class listview extends Component{

    constructor(props){
    super(props);
    var ds = ListView.DataSource({rowHasChanged : (r1,r2) => r1!==r2});
    this.state = {
    dataSource: ds.cloneWithRows(['1','2','3','4'])
    }
    }

    _renderRow(rowData){
      return <Text>{rowData}</Text>;
      }

    render(){
    return(
    <ListView
    dataSource = {this.state.dataSource}
    renderRow = {this._renderRow.bind(this)}
    />
    );
    }
    }
    AppRegistry.registerComponent('listview', () => listview );

您可以看到错误here。我该如何解决?

1 个答案:

答案 0 :(得分:4)

constructor(props){
  super(props);
  var ds = new ListView.DataSource({rowHasChanged : (r1,r2) => r1!==r2});
  //-------^^^ you forgot to place `new` keyword here.
  this.state = {
    dataSource: ds.cloneWithRows(['1','2','3','4'])
  }
}