Skip to content

Commit 8664657

Browse files
committed
fix: 🐛 migrate courses to netInfo
1 parent 6df6352 commit 8664657

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

challenges/data/03.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,60 @@
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
24+
```
25+
26+
- [ ] Create a new context `Network.tsx`
27+
28+
You should check if the user is connected to the internet and display a message when the user is not connected.
29+
30+
```javascript
31+
const NetworkContext = createContext<NetworkContextProps>(
32+
{} as NetworkContextProps
33+
);
34+
35+
export const NetworkProvider = ({ children }: PropsWithChildren) => {
36+
const [isConnected, setIsConnected] = useState<boolean | null>(false);
37+
38+
useEffect(() => {
39+
const unsubscribe = NetInfo.addEventListener((state) => {
40+
setIsConnected(state.isConnected);
41+
});
42+
43+
NetInfo.fetch().then((state) => {
44+
setIsConnected(state.isConnected);
45+
});
46+
47+
return () => {
48+
unsubscribe();
49+
};
50+
}, [isConnected]);
51+
52+
return (
53+
<NetworkContext.Provider value={{ isConnected }}>
54+
{children}
55+
</NetworkContext.Provider>
56+
);
57+
};
2358
```
2459

60+
You can use console.log to check if the user is connected to the internet.
61+
2562
- [ ] Update the entry point of your application to add a new provider
2663

2764
```javascript
28-
import { NetworkProvider } from 'react-native-offline';
65+
import { NetworkProvider } from '~/contexts/Network';
2966
```
3067

3168
`NetworkProvider` will injects the network state to children components via [React Context](https://reactjs.org/docs/context.html).

0 commit comments

Comments
 (0)