Skip to content

Instantly share code, notes, and snippets.

@hungdev
Forked from Chiamaka/OfflineNotice.js
Created August 18, 2018 06:42
Show Gist options
  • Save hungdev/270066036e51eebffb809b2259265497 to your computer and use it in GitHub Desktop.
Save hungdev/270066036e51eebffb809b2259265497 to your computer and use it in GitHub Desktop.
import React, { PureComponent } from 'react';
import { View, Text, NetInfo, Dimensions, StyleSheet } from 'react-native';
const { width } = Dimensions.get('window');
function MiniOfflineSign() {
return (
<View style={styles.offlineContainer}>
<Text style={styles.offlineText}>No Internet Connection</Text>
</View>
);
}
class OfflineNotice extends PureComponent {
state = {
isConnected: true
};
componentDidMount() {
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange);
}
componentWillUnmount() {
NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectivityChange);
}
handleConnectivityChange = isConnected => {
if (isConnected) {
this.setState({ isConnected });
} else {
this.setState({ isConnected });
}
};
render() {
if (!this.state.isConnected) {
return <MiniOfflineSign />;
}
return null;
}
}
const styles = StyleSheet.create({
offlineContainer: {
backgroundColor: '#b52424',
height: 30,
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
width,
position: 'absolute',
top: 30
},
offlineText: { color: '#fff' }
});
export default OfflineNotice;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment