Last active
March 4, 2021 18:24
-
-
Save kibolho/1391b2050da4d01fb76903b60bc37664 to your computer and use it in GitHub Desktop.
useLocalStorage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import AsyncStorage from '@react-native-community/async-storage'; | |
| class Storage { | |
| async set( | |
| key: string, | |
| value: string | Date | Record<string, unknown>, | |
| ): Promise<any> { | |
| return await AsyncStorage.setItem(key, JSON.stringify(value)); | |
| } | |
| async get(key: string): Promise<any> { | |
| const value = await AsyncStorage.getItem(key); | |
| if (value) { | |
| return JSON.parse(value); | |
| } | |
| return null; | |
| } | |
| async del(key: string): Promise<any> { | |
| return await AsyncStorage.removeItem(key); | |
| } | |
| } | |
| export default Storage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState, useEffect, useCallback } from 'react'; | |
| import Storage from './storage'; | |
| const getKeyValue = async ( | |
| storage: Storage, | |
| key: any, | |
| initialValue: any, | |
| setValue: (arg: any) => any, | |
| ): Promise<void> => { | |
| try { | |
| const item = await storage.get(key); | |
| setValue(item ?? initialValue); | |
| } catch (error) { | |
| return initialValue; | |
| } | |
| }; | |
| const useFetch = (storage: Storage, key: any, initialValue: any): any => { | |
| const [value, setValue] = useState(); | |
| useEffect(() => { | |
| getKeyValue(storage, key, initialValue, setValue); | |
| }, []); | |
| return value; | |
| }; | |
| function useLocalStorage( | |
| storage: Storage, | |
| key: string, | |
| initialValue: any, | |
| ): any { | |
| const fetchValue = useFetch(storage, key, initialValue); | |
| const [storedValue, setStoredValue] = useState(fetchValue); | |
| useEffect(() => { | |
| setStoredValue(fetchValue); | |
| }, [fetchValue]); | |
| const setValue = useCallback( | |
| (value) => { | |
| try { | |
| const valueToStore = | |
| value instanceof Function ? value(storedValue) : value; | |
| setStoredValue(valueToStore); | |
| storage.set(key, valueToStore); | |
| } catch (error) { | |
| console.log(error); | |
| } | |
| }, | |
| [key, storage, storedValue], | |
| ); | |
| return [storedValue || initialValue, setValue]; | |
| } | |
| export default useLocalStorage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment