Skip to content

Instantly share code, notes, and snippets.

@jketcham
Created May 8, 2017 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jketcham/cac3ed276c4fa440ab91c85e5dd7f4fc to your computer and use it in GitHub Desktop.
Save jketcham/cac3ed276c4fa440ab91c85e5dd7f4fc to your computer and use it in GitHub Desktop.
React browser network status indicator
const React = require('react');
class OfflineIndicator extends React.Component {
constructor(props) {
super(props);
this.handleOffline = this.handleOffline.bind(this);
this.state = { offline: false };
}
componentWillMount() {
window.addEventListener('offline', this.handleOffline);
window.addEventListener('online', this.handleOffline);
}
componentWillUnmount() {
window.removeEventListener('offline', this.handleOffline);
window.removeEventListener('online', this.handleOffline);
}
handleOffline(event) {
this.setState({ offline: event.type !== 'online' });
}
render() {
if (!this.state.offline) {
return null;
}
return (
<div className="hs-offline-indicator">
Oops, looks like you are not connected to the internet.
</div>
);
}
}
module.exports = OfflineIndicator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment