gpt4 book ai didi

react-native - React Native 评估'dataSource.rowIdentities

转载 作者:行者123 更新时间:2023-12-03 14:45:50 26 4
gpt4 key购买 nike

我正在使用 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();
}
}

然而,我得到了一个

undefined is not an object (evaluating 'dataSource.rowIdentities')



错误,尝试了很多方法也不起作用包含此 question ,有什么想法吗?

最佳答案

尝试将 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();
}
}

关于react-native - React Native 评估'dataSource.rowIdentities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35472324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com