Skip to content

Commit 71246ca

Browse files
authored
chore: Support react-native-netinfo 6 (#326)
1 parent 65e1cf5 commit 71246ca

12 files changed

+22
-19
lines changed

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"homepage": "https://github.com/rgommezz/react-native-offline#readme",
4343
"devDependencies": {
44-
"@react-native-community/netinfo": "^5.3.3",
44+
"@react-native-community/netinfo": "^6.0.0",
4545
"@types/enzyme": "^3.10.3",
4646
"@types/enzyme-adapter-react-16": "^1.0.5",
4747
"@types/jest": "^24.0.18",
@@ -103,7 +103,7 @@
103103
"redux-saga": "^1.0.2"
104104
},
105105
"peerDependencies": {
106-
"@react-native-community/netinfo": ">= 4.6 < 6",
106+
"@react-native-community/netinfo": ">= 6",
107107
"react-native": ">=0.60",
108108
"react-redux": ">=7.x",
109109
"redux": ">=3",

src/components/NetworkConnectivity.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type RequiredProps = {
1111
} & DefaultProps;
1212

1313
export type DefaultProps = ConnectivityArgs & {
14-
onConnectivityChange: (isConnected: boolean) => void;
14+
onConnectivityChange: (isConnected: boolean | null) => void;
1515
};
1616

1717
function validateProps(props: RequiredProps) {
@@ -58,7 +58,7 @@ class NetworkConnectivity extends React.PureComponent<
5858
super(props);
5959
validateProps(props);
6060
this.state = {
61-
isConnected: true,
61+
isConnected: null,
6262
};
6363
}
6464

src/components/ReduxNetworkProvider.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface AppState {
1414
type OwnProps = ConnectivityArgs;
1515

1616
interface StateProps {
17-
isConnected: boolean;
17+
isConnected: boolean | null;
1818
dispatch: Dispatch;
1919
}
2020

@@ -25,7 +25,7 @@ type Props = OwnProps &
2525
class ReduxNetworkProvider extends React.Component<Props> {
2626
static defaultProps = DEFAULT_ARGS;
2727

28-
handleConnectivityChange = (isConnected: boolean) => {
28+
handleConnectivityChange = (isConnected: boolean | null) => {
2929
const { isConnected: wasConnected, dispatch } = this.props;
3030
if (isConnected !== wasConnected) {
3131
dispatch(connectionChange(isConnected));

src/hooks/useIsConnected.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useContext } from 'react';
22
import NetworkContext from '../components/NetworkContext';
33

4-
export default function useIsConnected(): boolean {
4+
export default function useIsConnected(): boolean | null {
55
const context = useContext(NetworkContext);
66

77
if (!context) {

src/redux/actionCreators.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as actionTypes from './actionTypes';
22
import { EnqueuedAction, SemaphoreColor } from '../types';
33

4-
export const connectionChange = (isConnected: boolean) => ({
4+
export const connectionChange = (isConnected: boolean | null) => ({
55
type: actionTypes.CONNECTION_CHANGE as typeof actionTypes.CONNECTION_CHANGE,
66
payload: isConnected,
77
});

src/redux/createNetworkMiddleware.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ function checkIfActionShouldBeIntercepted(
7979
);
8080
}
8181

82-
function didComeBackOnline(action: EnqueuedAction, wasConnected: boolean) {
82+
function didComeBackOnline(
83+
action: EnqueuedAction,
84+
wasConnected: boolean | null,
85+
) {
8386
if ('type' in action && 'payload' in action) {
8487
return (
8588
action.type === networkActionTypes.CONNECTION_CHANGE &&
@@ -146,7 +149,7 @@ function createNetworkMiddleware({
146149
actionTypes,
147150
);
148151

149-
if (shouldInterceptAction && isConnected === false) {
152+
if (shouldInterceptAction && isConnected !== true) {
150153
// Offline, preventing the original action from being dispatched.
151154
// Dispatching an internal action instead.
152155
return next(fetchOfflineMode(action));

src/redux/sagas.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function* connectionHandler({
125125
pingServerUrl,
126126
httpMethod,
127127
customHeaders,
128-
}: NetInfoChangeArgs & { isConnected: boolean }) {
128+
}: NetInfoChangeArgs & { isConnected: boolean | null }) {
129129
if (shouldPing && isConnected) {
130130
yield fork(checkInternetAccessSaga, {
131131
pingTimeout,
@@ -219,7 +219,7 @@ export function* checkInternetAccessSaga({
219219
* - Flushes the queue of pending actions if we are connected back to the internet
220220
* @param hasInternetAccess
221221
*/
222-
export function* handleConnectivityChange(hasInternetAccess: boolean) {
222+
export function* handleConnectivityChange(hasInternetAccess: boolean | null) {
223223
const state: NetworkState = yield select(networkSelector);
224224
if (state.isConnected !== hasInternetAccess) {
225225
yield put(connectionChange(hasInternetAccess));

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface NetworkState extends ConnectivityState {
2525
}
2626

2727
export type ConnectivityState = {
28-
isConnected: boolean;
28+
isConnected: boolean | null;
2929
};
3030

3131
export type HTTPMethod = 'HEAD' | 'OPTIONS';

src/utils/checkInternetConnection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default async function checkInternetConnection(
2222
shouldPing = true,
2323
method: HTTPMethod = DEFAULT_HTTP_METHOD,
2424
customHeaders: HTTPHeaders = DEFAULT_CUSTOM_HEADERS,
25-
): Promise<boolean> {
25+
): Promise<boolean | null> {
2626
return NetInfo.fetch().then(async connectionState => {
2727
if (shouldPing) {
2828
const hasInternetAccess = await checkInternetAccess({

test/NetworkConnectivity.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('NetworkConnectivity', () => {
8888
it('passes the connection state into the FACC', () => {
8989
const children = jest.fn();
9090
shallow(getElement({ props: { children } }));
91-
expect(children).toHaveBeenCalledWith({ isConnected: true });
91+
expect(children).toHaveBeenCalledWith({ isConnected: null });
9292
});
9393

9494
describe('componentDidMount', () => {

test/NetworkConsumer.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ describe.only('NetworkConsumer', () => {
3434
it('receives isConnected prop from Provider using context', () => {
3535
const { getByTestId } = render(getElement({ children: <Consumer /> }));
3636
const textChild = getByTestId('connectionText');
37-
expect(textChild.props.children).toBe('Connected: true');
37+
expect(textChild.props.children).toBe('Connected: null');
3838
});
3939
});

0 commit comments

Comments
 (0)