diff options
| author | Akshay Nair <phenax5@gmail.com> | 2024-09-27 23:58:15 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2024-09-29 16:16:39 +0530 |
| commit | 5d456899ea444301870e1d3c721d139c9251c531 (patch) | |
| tree | bfb9d7a2d013be8d43a90bd4714147258ee3ddd6 /src/hooks | |
| parent | dda041a9aebc23f7a0576fc4030d06c71fa370d2 (diff) | |
| download | daft-launcher-5d456899ea444301870e1d3c721d139c9251c531.tar.gz daft-launcher-5d456899ea444301870e1d3c721d139c9251c531.zip | |
Fix too many updates issue with useFavorites + ui changes
Diffstat (limited to 'src/hooks')
| -rw-r--r-- | src/hooks/useFavorites.ts | 35 | ||||
| -rw-r--r-- | src/hooks/useStableCallback.ts | 9 |
2 files changed, 24 insertions, 20 deletions
diff --git a/src/hooks/useFavorites.ts b/src/hooks/useFavorites.ts index 54d77fd..619817c 100644 --- a/src/hooks/useFavorites.ts +++ b/src/hooks/useFavorites.ts @@ -1,11 +1,12 @@ -import {useCallback, useEffect, useState} from 'react'; +import {useEffect, useState} from 'react'; import {useAsyncStorage} from '@react-native-async-storage/async-storage'; +import {useStableCallback} from './useStableCallback'; export const useFavorites = () => { const favoritesStorage = useAsyncStorage('favorites'); const [favoriteAppNames, setFavoriteAppNames] = useState<string[]>([]); - const getFavoriteAppNames = useCallback(async (): Promise<string[]> => { + const getFavoriteAppNames = useStableCallback(async (): Promise<string[]> => { try { const result = await favoritesStorage.getItem(); const apps = result ? JSON.parse(result) : []; @@ -15,28 +16,22 @@ export const useFavorites = () => { console.error(err); return []; } - }, [favoritesStorage]); + }); - const refreshFavorites = useCallback(async () => { + const refreshFavorites = useStableCallback(async () => { getFavoriteAppNames().then(setFavoriteAppNames); - }, [getFavoriteAppNames]); + }); - const setFavorites = useCallback( - async (names: string[]) => { - const newNames = [...new Set(names)]; - setFavoriteAppNames(newNames); - await favoritesStorage.setItem(JSON.stringify(newNames)); - }, - [favoritesStorage], - ); + const setFavorites = useStableCallback(async (names: string[]) => { + const newNames = [...new Set(names)]; + setFavoriteAppNames(newNames); + await favoritesStorage.setItem(JSON.stringify(newNames)); + }); - const addToFavorites = useCallback( - async (packageName: string) => { - const names = await getFavoriteAppNames(); - setFavorites([...names, packageName]); - }, - [getFavoriteAppNames, setFavorites], - ); + const addToFavorites = useStableCallback(async (packageName: string) => { + const names = await getFavoriteAppNames(); + setFavorites([...names, packageName]); + }); useEffect(() => { refreshFavorites(); diff --git a/src/hooks/useStableCallback.ts b/src/hooks/useStableCallback.ts new file mode 100644 index 0000000..98e6c0d --- /dev/null +++ b/src/hooks/useStableCallback.ts @@ -0,0 +1,9 @@ +import {useCallback, useRef} from 'react'; + +export const useStableCallback = <Args extends any[], Ret>( + callback: (...a: Args) => Ret, +): ((...a: Args) => Ret) => { + const cb = useRef(callback); + cb.current = callback; + return useCallback((...args: Args) => cb.current(...args), [cb]); +}; |
