我正在使用React-Native中的dataSource创建一个基本的ListView,其中包含我获取的数据.
class Movies extends Component {
render() {
if (this.state.dataSource.getRowCount() === 0) {
return (
<View style={styles.container}>
<Text>
loading....
</Text>
</View>
);
}
else {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
/>
</View>
);
}
}
constructor(props){
super(props);
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
};
}
componentWillMount() {
this.fetchData();
}
fetchData(){
fetch(HOT_MOVIES_URL)
.then((response) => response.json())
.then((responseData) => {
if (responseData) {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData),
});
}
})
.done();
}
}
Run Code Online (Sandbox Code Playgroud)
然而,我得到了一个
undefined不是对象(评估'dataSource.rowIdentities')
错误,试过很多方法也不是包含这个问题的工作,有什么想法吗?
尝试将ListView DataSource设置为可重用变量,然后在需要时重复使用它.此外,您可能需要将dataSource设置为空数组,直到您的数据进入.尝试这样的事情:
var ds = new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2 })
class Movies extends Component {
render() {
if (this.state.dataSource.getRowCount() === 0) {
return (
<View style={styles.container}>
<Text>
loading....
</Text>
</View>
);
}
else {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
/>
</View>
);
}
}
constructor(props){
super(props);
this.state = {
dataSource: ds.cloneWithRows([]),
};
}
componentWillMount() {
this.fetchData();
}
fetchData(){
fetch(HOT_MOVIES_URL)
.then((response) => response.json())
.then((responseData) => {
if (responseData) {
this.setState({
dataSource: ds.cloneWithRows(responseData),
});
}
})
.done();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10021 次 |
| 最近记录: |