Skip to content

Commit 66d14ab

Browse files
authored
Merge pull request #242 from flexbox/fix/netInfo
fix: 🐛 migrate courses to netInfo
2 parents c5c0b46 + 71a69de commit 66d14ab

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

challenges/data/03.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,28 @@
99

1010
## 👾 Before we start the exercise
1111

12-
- [ ] We are going to install and use `react-native-offline`
13-
- [ ] Have a look at [`react-native-offline` documentation](https://github.com/rgommezz/react-native-offline)
12+
- [ ] We are going to install and use `@react-native-community/netinfo`
13+
- [ ] Have a look at [`@react-native-community/netinfo` documentation](https://github.com/react-native-netinfo/react-native-netinfo)
1414

1515
## 👨‍🚀 Exercise 3
1616

17-
![Offline Example](https://raw.githubusercontent.com/reactgraphqlacademy/twitter-clone-native/master/src/exercice/05/offline-example.gif)
18-
1917
- [ ] Install the library
2018

2119
```console
22-
npm install react-native-offline
20+
yarn add @react-native-community/netinfo
21+
```
22+
```console
23+
npm install --save @react-native-community/netinfo
2324
```
2425

26+
- [ ] Create a new file `Network.tsx` and [paste this component](https://raw.githubusercontent.com/flexbox/react-native-workshop/main/hackathon/spacecraft/src/components/exercice/Network.tsx)
27+
28+
You can use console.log to check if the user is connected to the internet.
29+
2530
- [ ] Update the entry point of your application to add a new provider
2631

2732
```javascript
28-
import { NetworkProvider } from 'react-native-offline';
33+
import { NetworkProvider } from '~/contexts/Network';
2934
```
3035

3136
`NetworkProvider` will injects the network state to children components via [React Context](https://reactjs.org/docs/context.html).
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import NetInfo from "@react-native-community/netinfo";
2+
import type { PropsWithChildren } from "react";
3+
import React, { createContext, useContext, useEffect, useState } from "react";
4+
5+
interface NetworkContextProps {
6+
isConnected: boolean | null;
7+
}
8+
9+
const NetworkContext = createContext<NetworkContextProps>(
10+
{} as NetworkContextProps
11+
);
12+
13+
export const NetworkProvider = ({ children }: PropsWithChildren) => {
14+
const [isConnected, setIsConnected] = useState<boolean | null>(false);
15+
16+
useEffect(() => {
17+
// use `NetInfo.addEventListener` to listen to network status changes
18+
// use `NetInfo.fetch` to get the current network status
19+
// store the network status in the `isConnected` state
20+
return () => {
21+
// unsubscribe from the network status listener
22+
};
23+
}, [isConnected]);
24+
25+
return (
26+
{/* Add `NetworkContext.Provider` with the connection status value */ }
27+
);
28+
};
29+
30+
export const useNetwork = () => useContext(NetworkContext);

0 commit comments

Comments
 (0)