Skip to content

Commit

Permalink
feature(useNetinfo): return null for initial unknown connection state (
Browse files Browse the repository at this point in the history
…#444 by @lisabaut)

BREAKING CHANGE: When the connection state is unknown, the `isConnected` and `isInternetConnected` properties are now set to `null` rather than `false`. This allow you to easily detect the initial "unknown" state before the state is detected and set to a `boolean`.

Fixes #295
  • Loading branch information
lisabaut authored and matt-oakes committed Feb 19, 2021
1 parent 7ba1a17 commit 4d84f14
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ Describes the current state of the network. It is an object with these propertie
| Property | Type | Description |
| --------------------- | --------------------------------------- | -------------------------------------------------------------------------------------------------- |
| `type` | [`NetInfoStateType`](#netinfostatetype) | The type of the current connection. |
| `isConnected` | `boolean` | If there is an active network connection. Note that this DOES NOT mean that internet is reachable. |
| `isInternetReachable` | `boolean` | If the internet is reachable with the currently active network connection. |
| `isConnected` | `boolean`, `null` | If there is an active network connection. If unknown defaults to `null`. |
| `isInternetReachable` | `boolean`, `null` | If the internet is reachable with the currently active network connection. If unknown defaults to `null` |
| `isWifiEnabled` | `boolean` | *(Android only)* Whether the device's WiFi is ON or OFF. |
| `details` | | The value depends on the `type` value. See below. |

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export function useNetInfo(

const [netInfo, setNetInfo] = useState<Types.NetInfoState>({
type: Types.NetInfoStateType.unknown,
isConnected: false,
isInternetReachable: false,
isConnected: null,
isInternetReachable: null,
details: null,
});

Expand Down
4 changes: 2 additions & 2 deletions src/internal/internetReachability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class InternetReachability {
}

private _setIsInternetReachable = (
isInternetReachable: boolean | null | undefined,
isInternetReachable: boolean | null,
): void => {
if (this._isInternetReachable === isInternetReachable) {
return;
Expand All @@ -41,7 +41,7 @@ export default class InternetReachability {
this._listener(this._isInternetReachable);
};

private _setExpectsConnection = (expectsConnection: boolean): void => {
private _setExpectsConnection = (expectsConnection: boolean | null): void => {
// Cancel any pending check
if (this._currentInternetReachabilityCheckHandler !== null) {
this._currentInternetReachabilityCheckHandler.cancel();
Expand Down
4 changes: 2 additions & 2 deletions src/internal/nativeModule.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ const getCurrentState = (
} else if (type === NetInfoStateType.unknown) {
const state: NetInfoUnknownState = {
...baseState,
isConnected: false,
isInternetReachable: false,
isConnected: null,
isInternetReachable: null,
type,
details: null,
};
Expand Down
12 changes: 8 additions & 4 deletions src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface NetInfoConnectedState<
> {
type: T;
isConnected: true;
isInternetReachable: boolean | null | undefined;
isInternetReachable: boolean | null;
details: D & NetInfoConnectedDetails;
isWifiEnabled?: boolean;
}
Expand All @@ -47,9 +47,13 @@ interface NetInfoDisconnectedState<T extends NetInfoStateType> {
details: null;
}

export type NetInfoUnknownState = NetInfoDisconnectedState<
NetInfoStateType.unknown
>;
export interface NetInfoUnknownState {
type: NetInfoStateType.unknown;
isConnected: null;
isInternetReachable: null;
details: null;
}

export type NetInfoNoConnectionState = NetInfoDisconnectedState<
NetInfoStateType.none
>;
Expand Down

0 comments on commit 4d84f14

Please sign in to comment.