我是新的反应本地人。我正在使用Android Studio 2.3.2进行模拟。我有以下代码,我认为这是相当标准的
size
但是我收到了错误
_reactNative2.default.DataSource不是构造函数。
答案 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);