_reactNative2.default.DataSource不是构造函数

时间:2017-05-20 11:20:14

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

我是新的反应本地人。我正在使用Android Studio 2.3.2进行模拟。我有以下代码,我认为这是相当标准的

size

但是我收到了错误

  

_reactNative2.default.DataSource不是构造函数。

2 个答案:

答案 0 :(得分:0)

this.DataSource

不是组件类的一部分在状态中使用它应该使用

const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = { dataSource: ds.cloneWithRows(['row 1', 'row 2']), };

然后在你的渲染功能

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

更新

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

export default class Tests extends Component {
componentWillMount() {
  this.state = {
    libraries: [1, 2, 3]
  }
   const ds = new ListView.DataSource({
   rowHasChanged: (r1, r2) => r1 !== r2
   });

   this.DataSource = ds.cloneWithRows(this.state.libraries);
}

renderRow(library) {
  return <Text>{library}</Text>
}

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

const styles = StyleSheet.create({
  container: {
  flex: 1,
  justifyContent: 'center',
  alignItems: 'center',
  backgroundColor: '#F5FCFF',
 },
});

AppRegistry.registerComponent('Tests', () => Tests);

`

更新2

现在我认为它我认为this.props.libraries不是一个数组,它是一个json对象 请参阅 not a constructor error

这只说了字符串和数组将使用新的关键字而不是json

upate 3

我还注意到你有this.prop.libraries

的拼写错误this.props.libraries

答案 1 :(得分:0)

问题由

解决

更换

 import ListView from 'react-native';

 import { ListView } from 'react-native';

并纠正

的语法错误
 this.DataSource = ds.cloneWithRows(this.prop.libraries);

 this.DataSource = ds.cloneWithRows(this.props.libraries);