Last active
March 12, 2019 21:05
-
-
Save jvandenaardweg/3bc51520a0b75d57dd9c43c3c7160dd7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React from 'react'; | |
import NetInfo from '@react-native-community/netinfo'; | |
export const NetworkContext = React.createContext({ isConnected: true }); | |
export class NetworkProvider extends React.PureComponent { | |
state = { | |
isConnected: true | |
}; | |
componentDidMount() { | |
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange); | |
} | |
componentWillUnmount() { | |
NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectivityChange); | |
} | |
handleConnectivityChange = isConnected => this.setState({ isConnected }); | |
render() { | |
return ( | |
<NetworkContext.Provider value={this.state}> | |
{this.props.children} | |
</NetworkContext.Provider> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment