上拉刷新下拉加载

RN实现上拉刷新下拉加载

第一步添加依赖
import React, { Component } from “react”;
import {
Platform,
StyleSheet,
Text,
Image,
ScrollView,
View
} from “react-native”;
import RefreshListView, { RefreshState } from “react-native-refresh-list-view”;

export default class Top1 extends Component {
constructor(props) {
super(props);

this.state = { data: [], page: 1, refreshState: RefreshState.Idle };

}
//生命周期
componentDidMount() {
this.onHeaderRefresh();
}

//上拉刷新
onHeaderRefresh = () => {
this.setState({ refreshState: RefreshState.HeaderRefreshing, page: 1 });
fetch(
https://cnodejs.org/api/v1/topics?page=${ this.state.page }&tab=good&limit=1
)
.then(response => response.json())
.then(responseJson => {
this.setState({
data: responseJson.data,
page: this.state.page + 1,
refreshState: RefreshState.Idle
});
})
.catch(error => {
console.log(error);
});
};

//下拉加载
onFooterRefresh = () => {
this.setState({ refreshState: RefreshState.HeaderRefreshing });
fetch(
https://cnodejs.org/api/v1/topics?page=${ this.state.page }&tab=good&limit=1
)
.then(response => response.json())
.then(responseJson => {
this.setState({
data: […this.state.data, …responseJson.data],
page: this.state.page + 1,
refreshState: RefreshState.Idle
});
})
.catch(error => {
console.log(error);
});
};
render() {
return (

<RefreshListView
data={this.state.data}
keyExtractor={(item, index) => {
index;
}}
renderItem={({ item }) => (
<View style={{ flexDirection: “row”, width: “100%”, height: 80 }}>
<Image
style={{ width: 60, height: 60, margin: 5 }}
source={{ uri: item.author.avatar_url }}
/>
<Text
style={{ fontSize: 18 }}
// onPress={() => {
// this.props.navigation.navigate(“Details”, {
// abc: item.content
// });
// }}
>
{item.title}


)}
refreshState={this.state.refreshState}
onHeaderRefresh={this.onHeaderRefresh}
onFooterRefresh={this.onFooterRefresh}
/>

);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: “center”,
alignItems: “center”,
backgroundColor: “#F5FCFF”
}
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值