数据解析-App

import React, { Component } from "react";

import { Platform, StyleSheet, Text, View } 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 = () => {

    //1.更新状态,下拉状态

    this.setState({

      refreshState: RefreshState.HeaderRefreshing,

      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 }) => (

            <Text

              style={{ width: "100%", height: 80 }}

              onPress={() => {

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

                  abc: item.content

                });

              }}

            >

              {item.title}

            </Text>

          )}

          refreshState={this.state.refreshState}

          onHeaderRefresh={this.onHeaderRefresh}

          onFooterRefresh={this.onFooterRefresh}

        />

      </View>

    );

  }

}

/**

* 样式表

*/

const styles = StyleSheet.create({

  container: {

    flex: 1,

    backgroundColor: "#F5FCFF"

  },

  welcome: {

    flex: 1

  }

});

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

推荐阅读更多精彩内容

  • 吃冰荔枝 是我的夏天
    大树BIG阅读 126评论 0 0
  • 等不到回复就睡觉吧!希望下次不要再这么傻了,虽然每次都在内心跟自己这么说,可是还是控制不住自己那颗跳动的心❤晚安!...
    董妮和余生阅读 210评论 0 0
  • 上午和顽皮在户外练功一次 天气晴好 心情也舒畅 练完又慢走了一圈回家 吸收了正能量 今天的茶泡的格外好喝 下午一趟...
    了了妈2017阅读 281评论 1 3