aboutsummaryrefslogtreecommitdiff
path: root/src/utils/hooks.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/hooks.ts')
-rw-r--r--src/utils/hooks.ts31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/utils/hooks.ts b/src/utils/hooks.ts
new file mode 100644
index 00000000..3914ef48
--- /dev/null
+++ b/src/utils/hooks.ts
@@ -0,0 +1,31 @@
+import AsyncStorage from '@react-native-community/async-storage';
+import {useEffect, useState} from 'react';
+
+export const useAsyncStorage = (key: string, defaultValue: string) => {
+ const [storedValue, setStoredValue] = useState<string>(defaultValue);
+
+ const getStoredItem = async (key: string, defaultValue: string) => {
+ try {
+ const item = await AsyncStorage.getItem(key);
+ const value = item ? item : defaultValue;
+ setStoredValue(value);
+ } catch (error) {
+ console.log(error);
+ }
+ };
+
+ useEffect(() => {
+ getStoredItem(key, defaultValue);
+ }, [key, defaultValue]);
+
+ const setValue = async (value: string) => {
+ try {
+ setStoredValue(value);
+ await AsyncStorage.setItem(key, value);
+ } catch (error) {
+ console.log(error);
+ }
+ };
+
+ return [storedValue, setValue];
+};