首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何删除网络请求失败错误屏幕并在react-native中显示消息"No internet connection“

如何删除网络请求失败错误屏幕并在react-native中显示消息"No internet connection“
EN

Stack Overflow用户
提问于 2018-06-07 15:34:39
回答 4查看 1.7K关注 0票数 1

如何删除网络请求失败的错误屏幕,并在没有互联网连接时显示消息"No internet connection“,以便在react-native中获得更好的用户体验。

EN

回答 4

Stack Overflow用户

发布于 2018-06-07 15:57:53

您可以在React-Native中使用NetInfo来检查网络状态。这是链接:https://facebook.github.io/react-native/docs/netinfo.html

以下是示例代码:

代码语言:javascript
复制
 NetInfo.getConnectionInfo().then((connectionInfo) => {
            if (connectionInfo.type === 'none') {
                alert("No internet connection")
            } else {
                // online
               // do something

            }
        });
票数 0
EN

Stack Overflow用户

发布于 2018-06-07 17:40:19

我在这里写了一个处理互联网状态问题的组件,参考下面的内容:

代码语言:javascript
复制
import React, { Component } from "react";
import {
  View,
  NetInfo,
  Animated,
  Easing,
  Dimensions,
  Platform,
  AppState
} from "react-native";
// import { colors, Typography, primaryFont } from "../../Config/StylesConfig";
import { connect } from "react-redux";
import { changeConnectionStatus } from "../../actions/authActions";
import CustomText from "./CustomText";
import firebaseHelper from "../../helpers/firebaseHelper";

const { width } = Dimensions.get("window");
class InternetStatusBar extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isNetworkConnected: true
    };
    this._updateConnectionStatus = this._updateConnectionStatus.bind(this);
    this.positionValue = new Animated.Value(-26);
    this.colorValue = new Animated.Value(0);
    this.isMount = true;
    this.isOnline = true;
  }

  _handleAppStateChange = nextAppState => {
    if (nextAppState.match(/inactive|background/) && this.isOnline) {
      firebaseHelper.goOffline();
      // console.log("offline");
      this.isOnline = false;
    } else if (nextAppState === "active" && this.isOnline === false) {
      firebaseHelper.goOnline();
      // console.log("online");
      this.isOnline = true;
    }
  };
  componentDidMount() {
    AppState.addEventListener("change", this._handleAppStateChange);
    // NetInfo.isConnected.fetch().done(isNetworkConnected => {
    //   this._updateConnectionStatus(isNetworkConnected);
    // });
    NetInfo.isConnected.addEventListener(
      "connectionChange",
      this._updateConnectionStatus
    );
  }

  componentWillUnmount() {
    AppState.removeEventListener("change", this._handleAppStateChange);
    NetInfo.isConnected.removeEventListener(
      "connectionChange",
      this._updateConnectionStatus
    );
  }

  _updateConnectionStatus(isNetworkConnected) {
    // this.setState({ isNetworkConnected });
    if (this.isMount) {
      this.isMount = false;
    } else {
      if (isNetworkConnected) {
        this.animateColorChange(isNetworkConnected);
        setTimeout(() => {
          this.animateErrorView(isNetworkConnected);
        }, 1000);
      } else {
        this.animateErrorView(isNetworkConnected);
        this.colorValue = new Animated.Value(0);
      }
    }
    // this.props.changeConnectionStatus(isNetworkConnected);
  }

  // componentWillReceiveProps = nextProps => {
  //   if (
  //     nextProps.isInternetConnected &&
  //     nextProps.isInternetConnected != this.state.isInternetConnected
  //   ) {
  //     const date = new Date();
  //     Actions.refresh({ refreshContent: date.getTime() });
  //   }
  // };

  animateErrorView(connected) {
    Animated.timing(this.positionValue, {
      toValue: connected ? -40 : Platform.OS === "ios" ? 20 : 0,
      easing: Easing.linear,
      duration: 600
    }).start();
  }

  animateColorChange(connected) {
    Animated.timing(this.colorValue, {
      toValue: connected ? 150 : 0,
      duration: 800
    }).start();
  }

  render() {
    return (
      <Animated.View
        style={[
          {
            position: "absolute",
            backgroundColor: this.colorValue.interpolate({
              inputRange: [0, 150],
              outputRange: ["rgba(0,0,0,0.6)", "rgba(75, 181, 67, 0.8)"]
            }),
            zIndex: 1,
            width: width,
            top: 0,
            transform: [{ translateY: this.positionValue }]
          }
        ]}
      >
        <View
          style={[
            {
              padding: 4,
              flexDirection: "row",
              flex: 1
            }
          ]}
        >
          <CustomText
            style={{
              fontSize: 12,
              textAlign: "center",
              flex: 1
            }}
          >
            {this.state.isInternetConnected ? "Back online" : "No connection"}
          </CustomText>
        </View>
      </Animated.View>
    );
  }
}

const mapStateToProps = state => {
  return {
    isInternetConnected: state.user.isInternetConnected
  };
};

export default connect(mapStateToProps, { changeConnectionStatus })(
  InternetStatusBar
);
票数 0
EN

Stack Overflow用户

发布于 2018-06-07 18:11:38

Snackbar是传达这一点的一个很好的方式。看看这个库:https://github.com/9gag-open-source/react-native-snackbar-dialog

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50735411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档