-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
50 lines (43 loc) · 1.1 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
import React, {
createContext,
Dispatch,
SetStateAction,
useCallback,
useEffect,
useState,
} from 'react';
import AppView from './src/components/AppView';
import {IPokemon} from './src/screens/ListScreen';
import {getStorageData, STORAGE_FAVORITE} from './src/utils/Storage';
interface IContext {
favorite: IPokemon | undefined;
setFavorite: Dispatch<SetStateAction<IPokemon | undefined>>;
}
export const AppContext = createContext<IContext>({
favorite: undefined,
setFavorite: () => {},
});
const App = () => {
const [favorite, setFavorite] = useState<IPokemon | undefined>();
const getFavoritePokemon = useCallback(async () => {
let result = await getStorageData(STORAGE_FAVORITE);
if (result) {
setFavorite(JSON.parse(result));
} else if (result === undefined) {
setFavorite(result);
}
}, []);
useEffect(() => {
getFavoritePokemon();
}, [getFavoritePokemon]);
return (
<AppContext.Provider
value={{
favorite: favorite,
setFavorite: setFavorite,
}}>
<AppView />
</AppContext.Provider>
);
};
export default App;