网络获取上啦刷新下拉加载

import React, { Component } from "react";

import { StyleSheet, Text, View, Image } from "react-native";

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

export default class App extends Component {

  constructor(props) {

    super(props);

    this.state = {

      dataValue: [],

      refreshState: RefreshState.Idle,

      page: 1

    };

  }

  componentDidMount() {

    this.onHeaderRefresh();

  }

  //上拉加载

  onHeaderRefresh = () => {

    this.setState({

      refreshState: RefreshState.FooterRefreshing,

      page: 1

    });

    //2网络加载

    fetch(

      `https://cnodejs.org/api/v1/topics?page=${

        this.state.page

      }&tab=good&limit=10`

    )

      .then(response => response.json())

      .then(responseJson => {

        this.setState({

          dataValue: responseJson.data,

          refreshState: RefreshState.Idle,

          page: this.state.page + 1

        });

      })

      .catch(error => {

        this.setState({

          refreshState: RefreshState.Failure

        });

        console.warn(error);

      });

  };

  //下拉加载更多

  onFooterRefresh = () => {

    this.setState({

      refreshState: RefreshState.FooterRefreshing

    });

    //2网络加载

    fetch(

      `https://cnodejs.org/api/v1/topics?page=${

        this.state.page

      }&tab=good&limit=10`

    )

      .then(response => response.json())

      .then(responseJson => {

        this.setState({

          dataValue: [...this.state.dataValue, ...responseJson.data],

          refreshState: RefreshState.Idle,

          page: this.state.page + 1

        });

      })

      .catch(error => {

        this.setState({

          refreshState: RefreshState.Failure

        });

        console.warn(error);

      });

  };

  render() {

    return (

      <View style={styles.container}>

        <RefreshListView

          style={styles.welcome}

          data={this.state.dataValue}

          keyExtractor={(item, index) => index}

          renderItem={({ item }) => (

            <View style={{ flexDirection: "row" }}>

              <Image

                source={{ uri: item.author.avatar_url }}

                style={{

                  width: 50,

                  height: 50,

                  borderRadius: 25,

                  marginRight: 10

                }}

              />

              <Text

                style={{ height: 80 }}

                onPress={() => {

                  this.props.navigation.navigate("Details", {

                    abc: item.content

                  });

                }}

              >

                {item.title}

              </Text>

            </View>

          )}

          refreshState={this.state.refreshState}

          onHeaderRefresh={this.onHeaderRefresh}

          onFooterRefresh={this.onFooterRefresh}

        />

      </View>

    );

  }

}

const styles = StyleSheet.create({

  container: {

    flex: 1,

    backgroundColor: "#F5FCFF"

  },

  welcome: {

    fontSize: 20,

    textAlign: "center",

    margin: 10,

    flexDirection: "row"

  }

});

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,483评论 0 10
  • 1 基础开发技巧 1.1 AppRegistry AppRegistry模块则是用来告知React Native哪...
    Kevin_Junbaozi阅读 1,516评论 0 2
  • 这两天状态有点差,心不在焉! 要不要出去释放一下负能量呢? 还是继续看书! 还是继续看书! 还是继续看书! 好吧!...
    一的倍数阅读 199评论 0 0
  • 感恩老妈,为我弟家看着孩子,为大侄子做饭,成天忙忙碌,还要操心小妹的事。做父母的真是一心为儿女。妈妈让我再跟进一下...
    寸心洁白阅读 133评论 2 1