Skip to content

Commit 37b6a3d

Browse files
Merge pull request #1 from baptistelambert/feat/connection-infos
Add connection info and children as a function
2 parents 1997721 + 798fbc5 commit 37b6a3d

File tree

5 files changed

+116
-20
lines changed

5 files changed

+116
-20
lines changed

README.md

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ import { NetInfoProvider } from 'react-native-netinfo';
2222
const App = () => (
2323
<View>
2424
<NetInfoProvider
25-
onChange={({ isConnected }) => {
25+
onChange={({ isConnected, connectionInfo }) => {
2626
console.log(isConnected);
27+
console.log(connectionInfo);
2728
}}
28-
render={({ isConnected }) =>
29+
render={({ isConnected, connectionInfo }) =>
2930
isConnected ? (
30-
<Text>Wonderful, you are connected!</Text>
31+
<React.Fragment>
32+
<Text>Wonderful, you are connected!</Text>
33+
<Text>Connection type: {connectionInfo.type}</Text>
34+
<Text>
35+
Effective connection type:{connectionInfo.effectiveType}
36+
</Text>
37+
</React.Fragment>
3138
) : (
3239
<Text>It looks like you encounter connectivity problems.</Text>
3340
)
@@ -37,23 +44,59 @@ const App = () => (
3744
);
3845
```
3946

47+
### With children as a function
48+
49+
```js
50+
import { NetInfoProvider } from 'react-native-netinfo';
51+
52+
const App = () => (
53+
<View>
54+
<NetInfoProvider
55+
onChange={({ isConnected, connectionInfo }) => {
56+
console.log(isConnected);
57+
console.log(connectionInfo);
58+
}}
59+
>
60+
{({ isConnected, connectionInfo }) =>
61+
isConnected ? (
62+
<React.Fragment>
63+
<Text>Wonderful, you are connected!</Text>
64+
<Text>Connection type: {connectionInfo.type}</Text>
65+
<Text>
66+
Effective connection type:{connectionInfo.effectiveType}
67+
</Text>
68+
</React.Fragment>
69+
) : (
70+
<Text>It looks like you encounter connectivity problems.</Text>
71+
)
72+
}
73+
</NetInfoProvider>
74+
</View>
75+
);
76+
```
77+
4078
### With component injection
4179

4280
```js
4381
import { NetInfoProvider } from 'react-native-netinfo';
4482

45-
const ConnectedComponent = ({ isConnected }) =>
83+
const ConnectedComponent = ({ isConnected, connectionInfo }) =>
4684
isConnected ? (
47-
<Text>Wonderful, you are connected!</Text>
85+
<React.Fragment>
86+
<Text>Wonderful, you are connected!</Text>
87+
<Text>Connection type: {connectionInfo.type}</Text>
88+
<Text>Effective connection type:{connectionInfo.effectiveType}</Text>
89+
</React.Fragment>
4890
) : (
4991
<Text>It looks like you encounter connectivity problems.</Text>
5092
);
5193

5294
const App = () => (
5395
<View>
5496
<NetInfoProvider
55-
onChange={({ isConnected }) => {
97+
onChange={({ isConnected, connectionInfo }) => {
5698
console.log(isConnected);
99+
console.log(connectionInfo);
57100
}}
58101
component={ConnectedComponent}
59102
/>
@@ -62,3 +105,22 @@ const App = () => (
62105
```
63106

64107
NB: you should not set both component and render props. If you were to do this, the render prop would be ignored.
108+
109+
## Constants
110+
111+
This package also exposes constants for connection info's types and effective types.
112+
113+
You can use them like so:
114+
115+
```js
116+
import { CONSTANTS } from 'react-native-netinfo';
117+
118+
const App = () => (
119+
<View>
120+
<Text>{CONSTANTS.CONNECTION_INFO.TYPES.WIFI}</Text>
121+
<Text>{CONSTANTS.CONNECTION_INFO.EFFECTIVE_TYPES['4G']}</Text>
122+
</View>
123+
);
124+
```
125+
126+
You can find the full list of types and effective types in the [official React Native documentation about NetInfo](https://facebook.github.io/react-native/docs/netinfo.html#connectiontype-enum).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-netinfo",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description":
55
"Notifies your app when the network goes offline and back online.",
66
"main": "src/index.js",

src/NetInfoProvider.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,55 @@
1-
import { createElement, Component } from 'react';
1+
import React, { createElement, Component } from 'react';
22
import { NetInfo } from 'react-native';
33

4+
const isEmptyChildren = children => React.Children.count(children) === 0;
5+
46
class NetInfoProvider extends Component {
57
static defaultProps = {
6-
render: () => null,
78
onChange: () => {}
89
};
910

1011
state = {
11-
isConnected: true
12+
isConnected: true,
13+
connectionInfo: {
14+
type: null,
15+
effectiveType: null
16+
}
1217
};
1318

1419
componentDidMount() {
15-
NetInfo.isConnected.addEventListener(
16-
'connectionChange',
17-
this.handleConnectionChange
18-
);
20+
NetInfo.addEventListener('connectionChange', this.handleConnectionChange);
1921
}
2022

2123
componentWillUnmount() {
22-
NetInfo.isConnected.removeEventListener(
24+
NetInfo.removeEventListener(
2325
'connectionChange',
2426
this.handleConnectionChange
2527
);
2628
}
2729

2830
handleConnectionChange = () => {
29-
NetInfo.isConnected.fetch().then(isConnected => {
30-
this.props.onChange({ isConnected });
31-
this.setState({ isConnected });
31+
Promise.all([
32+
NetInfo.isConnected.fetch(),
33+
NetInfo.getConnectionInfo()
34+
]).then(([isConnected, connectionInfo]) => {
35+
this.props.onChange({ isConnected, connectionInfo });
36+
this.setState({ isConnected, connectionInfo });
3237
});
3338
};
3439

3540
render() {
36-
const { component, render } = this.props;
41+
const { children, component, render } = this.props;
3742

3843
if (component) return createElement(component, this.state);
3944

40-
return render(this.state);
45+
if (render) return render(this.state);
46+
47+
if (typeof children === 'function') return children(this.state);
48+
49+
if (children && !isEmptyChildren(children))
50+
return React.Children.only(children);
51+
52+
return null;
4153
}
4254
}
4355

src/constants.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const CONSTANTS = {
2+
CONNECTION_INFO: {
3+
TYPES: {
4+
NONE: 'none',
5+
WIFI: 'wifi',
6+
CELLULAR: 'cellular',
7+
UNKNOWN: 'unknown',
8+
BLUETOOTH: 'bluetooth',
9+
ETHERNET: 'ethernet',
10+
WIMAX: 'wimax'
11+
},
12+
EFFECTIVE_TYPES: {
13+
'2G': '2g',
14+
'3G': '3g',
15+
'4G': '4g',
16+
UNKNOWN: 'unknown'
17+
}
18+
}
19+
};
20+
21+
export { CONSTANTS };

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { default as NetInfoProvider } from './NetInfoProvider';
2+
export { CONSTANTS } from './constants';

0 commit comments

Comments
 (0)