Android 在cloneWithRows中反应本机传递数组
我想将一个数组传递给cloneWithRows方法。 下面是代码Android 在cloneWithRows中反应本机传递数组,android,listview,react-native,react-jsx,Android,Listview,React Native,React Jsx,我想将一个数组传递给cloneWithRows方法。 下面是代码 export default class FirstReactNativeApp extends Component { constructor() { super(); const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); this.s
export default class FirstReactNativeApp extends Component {
constructor() {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
modalVisible: false,
todoText:'',
todoListArray:['foo 1','foo 2'],
dataSource: ds.cloneWithRows(this.state.todoListArray)
};
}
这是Listview
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{JSON.stringify(rowData)</Text>}
/>
我发现了错误
undefined不是对象“\u this.state.todolistaray”
我无法解决这个问题。请引导我
谢谢试试这个-
constructor() {
super();
this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
modalVisible: false,
todoText:'',
todoListArray:['foo 1','foo 2'],
};
}
在设置状态对象之前,您正在访问它。所以它返回undefined。像这样试试
export default class FirstReactNativeApp extends Component {
constructor() {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
modalVisible: false,
todoText:'',
todoListArray:['foo 1','foo 2'],
dataSource: ds.cloneWithRows(['foo 1','foo 2'])
};
}
然后,当todoListArray内容发生更改时,您可以将其设置为dataSource作为
维内尔给出的上述答案给了我一个线索
this.state = {
modalVisible: false,
todoText:'',
todoListArray:['foo 1','foo 2'],
dataSource: ds
};
然后在列表视图中
<ListView
dataSource= {this.state.dataSource.cloneWithRows(this.state.todoListArray)}
renderRow={(rowData) => <Text>{rowData}</Text>} />
它起作用了
谢谢在使用它时,它仍然给出了错误undefined不是一个objectevaluation'\u this.ds.cloneWithRows',这是因为您没有将const ds更改为this.ds
this.state = {
modalVisible: false,
todoText:'',
todoListArray:['foo 1','foo 2'],
dataSource: ds
};
<ListView
dataSource= {this.state.dataSource.cloneWithRows(this.state.todoListArray)}
renderRow={(rowData) => <Text>{rowData}</Text>} />