调用RefreshControl后,ListView数据不会更新

时间:2018-01-03 20:31:55

标签: reactjs listview react-native mobile

我可以在列表上刷新并接收来自网络API的请求/响应(在日志中),刷新状态会更改,但列表中的值不会更新,任何帮助都将受到赞赏。我可以看到RefreshControl没有错误,我相信我正确传递数据,有人可以帮助我吗?

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>
    );
}

1 个答案:

答案 0 :(得分:0)

解决方案:只是意识到我没有在fetch调用中添加headers: { 'Cache-Control': 'no-cache, no-store' }。使用的数据是缓存的数据。