-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.js
59 lines (50 loc) · 1.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict'
import React, { Component } from 'react'
import {
NetInfo,
StyleSheet,
Text,
View
} from 'react-native'
const RequiresConnection = (WhenOnline, WhenOffline) => class RequiresConnection extends Component {
constructor (props) {
super(props)
this.state = { isConnected: true }
}
componentDidMount () {
let connect = ({ type }) => this.setState({ isConnected: type !== 'none' })
NetInfo.getConnectionInfo().done((connectionInfo) => {
connect({ type: connectionInfo.type })
NetInfo.addEventListener('connectionChange', connect)
})
}
componentWillUnmount () {
NetInfo.removeEventListener('connectionChange')
}
render () {
if (this.state.isConnected) {
return <WhenOnline {...this.props} />
} else if (typeof WhenOffline === 'function') {
return <WhenOffline {...this.props} />
} else {
const message = WhenOffline || "We're sorry, there seems to be a problem with your internet connection. The application will resume as soon as it is able to reconnect to the internet."
return <View style={styles.container}>
<Text style={styles.connectionProblemMessage}>
{message}
</Text>
</View>
}
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
connectionProblemMessage: {
padding: 24,
textAlign: 'center'
}
})
module.exports = RequiresConnection