-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlineButton.js
More file actions
50 lines (45 loc) · 1.2 KB
/
OnlineButton.js
File metadata and controls
50 lines (45 loc) · 1.2 KB
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
import React from "react";
import { Alert, NetInfo, Platform, TouchableOpacity } from "react-native";
import * as strings from './strings';
export default class OnlineButton extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
checkConnectivity = () => {
// For Android devices
if (Platform.OS === "android") {
NetInfo.isConnected.fetch().then(isConnected => {
if (isConnected) {
this.props.onPress();
} else {
Alert.alert("Offline", strings.OFFLINE_TEXT);
}
});
} else {
// For iOS devices
NetInfo.isConnected.addEventListener(
"connectionChange",
this.handleFirstConnectivityChange
);
}
};
handleFirstConnectivityChange = isConnected => {
NetInfo.isConnected.removeEventListener(
"connectionChange",
this.handleFirstConnectivityChange
);
if (isConnected === false) {
Alert.alert("Offline", strings.OFFLINE_TEXT);
} else {
this.props.onPress();
}
};
render() {
return (
<TouchableOpacity style={this.props.style} onPress={this.checkConnectivity}>
{this.props.children}
</TouchableOpacity>
);
}
}