【发布时间】:2018-01-04 20:21:45
【问题描述】:
我知道有 1000 个类似的问题,但即使尝试不同的示例,我也无法解决这个问题,请提前道歉。
当我拉动以刷新我的数据时,会发送请求并收到新的响应(根据我的日志进行了验证)。但是,此数据并未反映在列表本身中。我知道列表视图足够聪明,可以在数据源发生更改时刷新数据,但这里似乎没有发生。
constructor(props) {
super(props);
const dataSource = new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2});
this.state = {
dataSource: dataSource.cloneWithRows([]),
refreshing: false
};
this._renderRow = this._renderRow.bind(this);
this._getCoinData = this._getCoinData.bind(this);
}
componentWillMount() {
this._getCoinData();
}
_getCoinData() {
return new Promise((resolve) => {
getCryptocurrencyData()
.then(function (result) {
const ds = new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2});
this.setState({
dataSource: ds.cloneWithRows(result),
jsonData: result
});
resolve();
}.bind(this))
});
}
_renderRow(data) {
return (
<CoinCell
coinName={data.name}
coinPrice={data.price_gbp}
coinPercentageChange={data.percent_change_24h}>
</CoinCell>)
}
_renderHeader() {
return (
<Header />
)
}
_onRefresh() {
this.setState({refreshing: true});
this._getCoinData()
.then(() => {
this.setState({refreshing: false});
});
}
render() {
return (
<View>
<ListView
enableEmptySections
refreshControl={
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this._onRefresh.bind(this)}
/>
}
ref={'resultListView'}
dataSource={this.state.dataSource}
renderRow={this._renderRow}
renderSeparator={(sectionId, rowId) => <View key={rowId} style={styles.separator}/>}
renderHeader={() => this._renderHeader()}
/>
</View>
);
}
}
【问题讨论】:
-
包括一个示例
result被传递到ds.cloneWithRows(result)。您似乎希望在填充数据源时使用数组,但您传递的是原始 JSON。 -
这是
result:pastebin.com/5FKgCTVV 的简化示例,这是fetch()方法:return new Promise(function (resolve, reject) { fetch(url, { headers: { 'Cache-Control': 'no-cache, no-store' } }) .then((response) => resolve(response.json())) .catch((error) => { console.error(error); }); })
标签: android reactjs listview react-native mobile