Skip to content

Instantly share code, notes, and snippets.

@ritikrishu
Last active June 3, 2019 07:46
Show Gist options
  • Save ritikrishu/1a866e4f720408e057273df387762b44 to your computer and use it in GitHub Desktop.
Save ritikrishu/1a866e4f720408e057273df387762b44 to your computer and use it in GitHub Desktop.
React native offline screen wrapper example
export default function connectionAwareActivity(WrappedComponent) {
return class extends PureComponent {
constructor(props) {
super(props);
this.state = { isConnected: false };
this.switchConnectivity = this.switchConnectivity.bind(this);
}
static navigationOptions = WrappedComponent.navigationOptions
componentDidMount() {
NetInfo.addEventListener('connectionChange', this.switchConnectivity);
NetInfo.isConnected.fetch().then(isConnected => {
this.setState({ isConnected });
});
}
componentWillUnmount(){
NetInfo.removeEventListener('connectionChange', this.switchConnectivity);
}
switchConnectivity(type) {
if(type === 'none'){
this.setState({isConnected: false})
}
else{
this.setState({isConnected: true})
}
}
render() {
return this.state.isConnected ? (
<WrappedComponent {...this.props} />
) : (
<OfflineScreen/>
);
}
};
}
@ritikrishu
Copy link
Author

@tejashwikalptaru yes it is, should use getConnectionInfo instead, but both API's return promise or take callback, this is just example code to represent the problem.
I've already tried componentWillMount but placing console.log('render was called') in render function still logs twice on screen navigation.
render

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment