forked from rauchy/react-native-offline-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (47 loc) · 1.3 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
'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 = (reach) => this.setState({ isConnected: reach !== 'none' })
NetInfo.fetch().done((reach) => {
connect(reach)
NetInfo.addEventListener('change', connect)
})
}
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