Guest User

Untitled

a guest
Jan 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. export default function connectionAwareActivity(WrappedComponent) {
  2. return class extends PureComponent {
  3. constructor(props) {
  4. super(props);
  5. this.state = { isConnected: false };
  6. this.switchConnectivity = this.switchConnectivity.bind(this);
  7. }
  8. static navigationOptions = WrappedComponent.navigationOptions
  9. componentDidMount() {
  10. NetInfo.addEventListener('connectionChange', this.switchConnectivity);
  11. NetInfo.isConnected.fetch().then(isConnected => {
  12. this.setState({ isConnected });
  13. });
  14. }
  15. componentWillUnmount(){
  16. NetInfo.removeEventListener('connectionChange', this.switchConnectivity);
  17. }
  18. switchConnectivity(type) {
  19. if(type === 'none'){
  20. this.setState({isConnected: false})
  21. }
  22. else{
  23. this.setState({isConnected: true})
  24. }
  25. }
  26. render() {
  27. return this.state.isConnected ? (
  28. <WrappedComponent {...this.props} />
  29. ) : (
  30. <OfflineScreen/>
  31. );
  32. }
  33. };
  34. }
Add Comment
Please, Sign In to add comment