Skip to content

Instantly share code, notes, and snippets.

@amorenew
Created October 8, 2017 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amorenew/8a58c8230e716b07f48f54e1d325e0a2 to your computer and use it in GitHub Desktop.
Save amorenew/8a58c8230e716b07f48f54e1d325e0a2 to your computer and use it in GitHub Desktop.
import React, {Component} from 'react';
import {StyleSheet, View, Text, NetInfo} from 'react-native';
import PropTypes from 'prop-types';
class InternetToast extends Component {
constructor(props) {
super(props);
this.state = {
isOnline: false
};
this.handleConnectivityChange = this
.handleConnectivityChange
.bind(this);
}
handleConnectivityChange(isConnected) {
console.log('Internet is ' + (isConnected
? 'online'
: 'offline'));
this.setState({isOnline: isConnected});
}
componentWillMount() {
NetInfo
.isConnected
.fetch()
.then(isConnected => this.handleConnectivityChange(isConnected));
NetInfo
.isConnected
.addEventListener('connectionChange', this.handleConnectivityChange);
}
componentWillUnmount() {
NetInfo
.isConnected
.removeEventListener('connectionChange', this.handleConnectivityChange);
}
render() {
var toastHeight = this.state.isOnline
? 0
: 45;
return <Text
style={{
width: "100%",
height: toastHeight,
backgroundColor: "#CE2029",
justifyContent: 'center',
alignItems: 'center',
fontSize: 16,
textShadowColor: '#AAA',
color: 'white',
textAlign: 'center',
textAlignVertical: 'center',
position:'absolute',
bottom:0
}}>No Internet Connection</Text>
}
}
InternetToast.prototypes = {
onConnectionChange: PropTypes.func.isRequired
}
export default InternetToast;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment