Skip to content

Commit 81fedc6

Browse files
authored
Exporting checkInternetConnection for queries on demand + docs (#99)
1 parent aab6455 commit 81fedc6

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Check out [this medium article](https://blog.callstack.io/your-react-native-offl
1919
+ [`Network reducer`](#network-reducer)
2020
+ [`createNetworkMiddleware()`](#createnetworkmiddleware)
2121
+ [`Offline Queue`](#offline-queue)
22+
* [Other Utilities](#other-utilities)
23+
+ [`checkInternetConnection`](#checkinternetconnection)
2224
* [Miscellanea](#miscellanea)
2325
* [FAQ](#faq)
2426
* [Contributions](#contributions)
@@ -408,6 +410,31 @@ fetchData.meta = {
408410
}
409411
```
410412

413+
### Other utilities
414+
415+
#### `checkInternetConnection()`
416+
Utility function that allows you to query for internet connectivity on demand. If you have integrated this library with redux, you can then dispatch a `CONNECTION_CHANGE` action type to inform the `network` reducer accordingly and keep it up to date. Check the example below.
417+
418+
```js
419+
checkInternetConnection(timeout?: number = 3000, url?: string = 'http://www.google.com/'): Promise<boolean>
420+
```
421+
422+
##### Example
423+
424+
```js
425+
import { checkInternetConnection, offlineActionTypes } from 'react-native-offline';
426+
427+
async function internetChecker(dispatch) {
428+
const isConnected = await checkInternetConnection();
429+
// Dispatching can be done inside a connected component, a thunk (where dispatch is injected), saga, or any sort of middleware
430+
// In this example we are using a thunk
431+
dispatch({
432+
type: offlineActionTypes.CONNECTION_CHANGE,
433+
payload: isConnected,
434+
});
435+
}
436+
```
437+
411438
## Miscellanea
412439

413440
### FAQ
@@ -427,7 +454,7 @@ As you can see in the snippets below, we create the `store` instance as usual an
427454
import { AsyncStorage, Platform, NetInfo } from 'react-native';
428455
import { createStore, applyMiddleware, compose } from 'redux';
429456
import { persistStore, autoRehydrate } from 'redux-persist';
430-
import { createNetworkMiddleware, offlineActionTypes, checkInternetConnectionOnStartup } from 'react-native-offline';
457+
import { createNetworkMiddleware, offlineActionTypes, checkInternetConnection } from 'react-native-offline';
431458
import rootReducer from '../reducers';
432459

433460
const networkMiddleware = createNetworkMiddleware();
@@ -450,7 +477,7 @@ export default function configureStore(callback) {
450477
},
451478
() => {
452479
// After rehydration completes, we detect initial connection
453-
checkInternetConnectionOnStartup().then(isConnected => {
480+
checkInternetConnection().then(isConnected => {
454481
store.dispatch({
455482
type: offlineActionTypes.CONNECTION_CHANGE,
456483
payload: isConnected,

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "react-native-offline",
3-
"version": "3.9.1",
3+
"version": "3.10.0",
44
"description": "Handy toolbelt to deal with offline mode in React Native applications. Cross-platform, provides a smooth redux integration.",
55
"main": "./src/index.js",
6-
"author": "Raul Gomez Acuña <[email protected]> (https://github.com/rauliyohmc)",
6+
"author": "Raul Gomez Acuña <[email protected]> (https://github.com/rgommezz)",
77
"license": "MIT",
88
"scripts": {
99
"lint": "eslint src",
@@ -17,7 +17,7 @@
1717
},
1818
"repository": {
1919
"type": "git",
20-
"url": "git+https://github.com/rauliyohmc/react-native-offline.git"
20+
"url": "git+https://github.com/rgommezz/react-native-offline.git"
2121
},
2222
"keywords": [
2323
"react-native",
@@ -30,9 +30,9 @@
3030
"src"
3131
],
3232
"bugs": {
33-
"url": "https://github.com/rauliyohmc/react-native-offline/issues"
33+
"url": "https://github.com/rgommezz/react-native-offline/issues"
3434
},
35-
"homepage": "https://github.com/rauliyohmc/react-native-offline#readme",
35+
"homepage": "https://github.com/rgommezz/react-native-offline#readme",
3636
"devDependencies": {
3737
"babel-eslint": "^7.2.1",
3838
"babel-jest": "^19.0.0",

src/checkInternetConnectionOnStartup.js renamed to src/checkInternetConnection.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
import { Platform, NetInfo } from 'react-native';
44
import checkInternetAccess from './checkInternetAccess';
55

6-
// on iOS, the listener is fired immediately after registration
7-
// on Android, we need to use `isConnected.fetch`, that returns a promise which resolves with a boolean
8-
export default function checkInternetConnectionOnStartup(
6+
/**
7+
* Utility that allows to query for internet connectivity on demand
8+
* On iOS, the listener is fired immediately after registration
9+
* On Android, we need to use `isConnected.fetch`, that returns a promise which resolves with a boolean
10+
* @param timeout
11+
* @param url
12+
* @returns {Promise<boolean>}
13+
*/
14+
export default function checkInternetConnection(
915
timeout: number = 3000,
1016
url: string = 'http://www.google.com/',
1117
): Promise<boolean> {

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
get networkEventsListenerSaga() {
1818
return require('./sagas').default;
1919
},
20-
get checkInternetConnectionOnStartup() {
21-
return require('./checkInternetConnectionOnStartup').default;
20+
get checkInternetConnection() {
21+
return require('./checkInternetConnection').default;
2222
},
2323
};

0 commit comments

Comments
 (0)