Rn 网络解析上拉刷新,下拉加载

import React, { Component } from "react";
import {
  Platform,
  StyleSheet,
  Text,
  View,
  FlatList,
  TouchableOpacity,
  Image
} from "react-native";

import { withNavigation } from "react-navigation";
import RefreshListView, { RefreshState } from "react-native-refresh-list-view";
class To extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tab: props.tab,
      limit: 15,
      page: 1,
      data: [],
      refreshState: RefreshState.Idle
    };
  }
  requstData() {
    return fetch(
      "https://cnodejs.org/api/v1/topics?tab=" +
        this.state.tab +
        "&page=" +
        this.page +
        "&limit=" +
        this.state.limit +
        " "
    )
      .then(response => response.json())
      .then(responseJson => {
        return responseJson.data;
      });
  }
componentDidMount() {
    this.requFirData();
  }
 requFirData = () => {
    try {
      this.setState(
        {
          refreshState: RefreshState.HeaderRefreshing,
          page: 1
        },
        async () => {
          let data = await this.requstData();
          this.setState({
            data: data,
            refreshState: RefreshState.Idle
          });
        }
      );
    } catch (error) {}
  };
footer = () => {
    try {
      this.setState(
        {
          refreshState: RefreshState.FooterRefreshing,
          page: this.state.page + 1
        },
        async () => {
          let data = await this.requstData();
          this.setState({
            data: [...this.state.data, ...data],
            refreshState:
              this.state.data.length > 30
                ? RefreshState.NoMoreData
                : RefreshState.Idle
          });
        }
      );
    } catch (error) {
      this.setState({
        refreshState: RefreshState.Failure
      });
    }
  };
 render() {
    return (
      <RefreshListView
        data={this.state.data}
        refreshState={this.state.refreshState}
        onHeaderRefresh={this.requFirData}
        onFooterRefresh={this.footer}
        renderItem={({ item, index }) => (
          <TouchableOpacity
            onPress={() =>
              this.props.navigation.navigate("Wan", {
                a: item.content,
                b: item.title,
                c: item.last_reply_at
              })
            }
          >
            <View style={style.hander}>
              <Image
                style={style.img}
                source={{ uri: item.author.avatar_url }}
              />

              <View>
                <Text>{item.title}</Text>
                <Text>{item.last_reply_at}</Text>
              </View>
            </View>
          </TouchableOpacity>
        )}
      />
    );
  }
}
const style = StyleSheet.create({
  hander: {
    flexDirection: "row"
  },
  img: {
    width: 100,
    height: 100
  }
});

export default withNavigation(To);

完整代码
import React, { Component } from "react";
import {
  Platform,
  StyleSheet,
  Text,
  View,
  FlatList,
  TouchableOpacity,
  Image
} from "react-native";

import { withNavigation } from "react-navigation";
import RefreshListView, { RefreshState } from "react-native-refresh-list-view";

class To extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tab: props.tab,
      limit: 15,
      page: 1,
      data: [],
      refreshState: RefreshState.Idle
    };
  }
  requstData() {
    return fetch(
      "https://cnodejs.org/api/v1/topics?tab=" +
        this.state.tab +
        "&page=" +
        this.page +
        "&limit=" +
        this.state.limit +
        " "
    )
      .then(response => response.json())
      .then(responseJson => {
        return responseJson.data;
      });
  }
  componentDidMount() {
    this.requFirData();
  }
  requFirData = () => {
    try {
      this.setState(
        {
          refreshState: RefreshState.HeaderRefreshing,
          page: 1
        },
        async () => {
          let data = await this.requstData();
          this.setState({
            data: data,
            refreshState: RefreshState.Idle
          });
        }
      );
    } catch (error) {}
  };
  footer = () => {
    try {
      this.setState(
        {
          refreshState: RefreshState.FooterRefreshing,
          page: this.state.page + 1
        },
        async () => {
          let data = await this.requstData();
          this.setState({
            data: [...this.state.data, ...data],
            refreshState:
              this.state.data.length > 30
                ? RefreshState.NoMoreData
                : RefreshState.Idle
          });
        }
      );
    } catch (error) {
      this.setState({
        refreshState: RefreshState.Failure
      });
    }
  };
  render() {
    return (
      <RefreshListView
        data={this.state.data}
        refreshState={this.state.refreshState}
        onHeaderRefresh={this.requFirData}
        onFooterRefresh={this.footer}
        renderItem={({ item, index }) => (
          <TouchableOpacity
            onPress={() =>
              this.props.navigation.navigate("Wan", {
                a: item.content,
                b: item.title,
                c: item.last_reply_at
              })
            }
          >
            <View style={style.hander}>
              <Image
                style={style.img}
                source={{ uri: item.author.avatar_url }}
              />

              <View>
                <Text>{item.title}</Text>
                <Text>{item.last_reply_at}</Text>
              </View>
            </View>
          </TouchableOpacity>
        )}
      />
    );
  }
}
const style = StyleSheet.create({
  hander: {
    flexDirection: "row"
  },
  img: {
    width: 100,
    height: 100
  }
});

export default withNavigation(To);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值