-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
58 lines (50 loc) · 1.75 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React, { useEffect } from "react";
import { SafeAreaView, StatusBar, View } from "react-native";
import { RecoilRoot, useRecoilState, useSetRecoilState } from "recoil";
import Header from "./src/components/Header";
import imageState from "./src/state/imageState";
import Body from "./src/components/Body";
import suggestionState from "./src/state/suggestionState";
import { fetchImages, fetchSuggestion } from "./src/fetchData";
import { Toast } from "react-native-toast-message/lib/src/Toast";
import internetState from "./src/state/internetState";
import NetInfo from "@react-native-community/netinfo";
const AppContent = () => {
const setImages = useSetRecoilState(imageState);
const setSuggestion = useSetRecoilState(suggestionState);
const [hasInternet, setHasInternet] = useRecoilState(internetState);
useEffect(() => {
// Internet check
const unsubscribe = NetInfo.addEventListener((state) => {
console.log(`Internet state: ${state.type} ${state.isInternetReachable}`);
setHasInternet(state.isInternetReachable);
});
// Fetch getImages and prompt suggestion
(async () => {
const images = await fetchImages();
const suggestion = await fetchSuggestion();
setImages(images ?? []); // Provide an empty array as the default value
setSuggestion(suggestion ?? ""); // Provide an empty string as the default value
})();
return () => {
unsubscribe(); // Stop internet connection check on unmount
};
}, []);
return (
<View className="flex-1">
<SafeAreaView />
<Header />
<Body />
<StatusBar barStyle={"dark-content"} />
</View>
);
};
const App = () => {
return (
<RecoilRoot>
<AppContent />
<Toast />
</RecoilRoot>
);
};
export default App;