我想将数组传递到clonewithrows方法。以下是代码
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>}
/>
我遇到了错误未定义不是对象(评估'_this.state.todolistarray')
我无法解决这个。请指导我!
谢谢
尝试这个 -
constructor() {
super();
this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
modalVisible: false,
todoText:'',
todoListArray:['foo 1','foo 2'],
};
}
<ListView
dataSource={this.ds.cloneWithRows(this.state.todoListArray)}
renderRow={(rowData) => <Text>{JSON.stringify(rowData)</Text>}
/>
您在设置它之前先访问state
对象。因此它返回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.setState({
dataSource: this.state.dataSource.cloneWithRows(this.state.todoListArray),
});
Vinayr给出的上述答案给了我一个线索。
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>} />
它有效!
谢谢