在 cloneWithRows 中反应本机传递数组
react native passing array in cloneWithRows
我想将一个数组传递给 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
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{JSON.stringify(rowData)</Text>}
/>
我遇到了错误
undefined 不是一个对象(评估'_this.state.todoListArray')
我无法解决this.Please指导我!
谢谢
试试这个 -
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
对象之前访问它。所以它 returns 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>} />
有效!
谢谢
我想将一个数组传递给 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
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{JSON.stringify(rowData)</Text>}
/>
我遇到了错误 undefined 不是一个对象(评估'_this.state.todoListArray')
我无法解决this.Please指导我!
谢谢
试试这个 -
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
对象之前访问它。所以它 returns 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>} />
有效!
谢谢