fix(localStorage): solution to localStorage issue

Still need to find a good way to re-render
This commit is contained in:
Christopher committed 2021-01-08 21:48:24 +01:00
1 parent 07dc73f0cb
commit 5fdb9ba21c
7 files changed
+51 -53

No files matched your search

-7
View File
@@ -1,7 +0,0 @@
import { useRecoilState } from 'recoil';
import { phoneState } from './state';
export const useForceUpdate = () => {
const [update, setUpdate] = useRecoilState(phoneState.forceUpdateState);
return { update, setUpdate };
};
@@ -1,12 +0,0 @@
export const getStorageItem = (key: string) => {
const UNIQ_PREFIX = 'npwd_phone_';
try {
const data = window.localStorage.getItem(UNIQ_PREFIX + key);
// return stored or initValue if none stored
return JSON.parse(data);
} catch (e) {
// TODO: Integrate debug error state here
console.log(e);
// We still return initVal if error is thrown
}
};
+22 -2
View File
@@ -9,7 +9,7 @@ import { useState } from 'react';
* if there is no savedState
*/
function useLocalStorage<T>(key?: string, initialValue?: T) {
function useLocalStorage<T>(key: string, initialValue: T) {
// Serves as a nice identifying prefix for the key
const UNIQ_PREFIX = 'npwd_phone_';
const [localVal, setLocalVal] = useState<T>(() => {
@@ -43,7 +43,27 @@ function useLocalStorage<T>(key?: string, initialValue?: T) {
}
};
return [localVal, setVal] as const;
return [localVal, setVal];
}
export const writeStorage = <T>(key: string, value: T) => {
const UNIQ_PREFIX = 'npwd_phone_';
try {
window.localStorage.setItem(UNIQ_PREFIX + key, JSON.stringify(value));
} catch (error) {
console.log(error);
}
};
export const getStorage = (key: string) => {
const UNIQ_PREFIX = 'npwd_phone_';
try {
const data = window.localStorage.getItem(UNIQ_PREFIX + key);
return JSON.parse(data);
} catch (error) {
console.log(error);
}
};
export default useLocalStorage;