Created
May 8, 2017 17:37
-
-
Save jketcham/cac3ed276c4fa440ab91c85e5dd7f4fc to your computer and use it in GitHub Desktop.
React browser network status indicator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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