feat(localstorage): resolved issue with phone not re-rendering

This commit is contained in:
Christopher committed 2021-01-04 20:33:53 +01:00
1 parent 14223c9800
commit dddddc01f8
5 files changed
+19 -6

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
import { useRecoilState } from 'recoil';
import { phoneState } from './state';
export const useForceUpdate = () => {
const [update, setUpdate] = useRecoilState(phoneState.forceUpdateState);
return { update, setUpdate };
};
+4
View File
@@ -9,4 +9,8 @@ export const phoneState = {
key: 'phoneConfig',
default: undefined,
}),
forceUpdateState: atom({
key: 'fuckingupdate',
default: 0,
}),
};
@@ -1,4 +1,5 @@
import { useState } from 'react';
import { useForceUpdate } from '../../../os/phone/hooks/forceUpdate';
// TODO: Integrate debug log to fetch when that branch is merged
@@ -10,6 +11,7 @@ import { useState } from 'react';
*/
function useLocalStorage<T>(key?: string, initialValue?: T) {
const { update, setUpdate } = useForceUpdate();
// Serves as a nice identifying prefix for the key
const UNIQ_PREFIX = 'npwd_phone_';
const [localVal, setLocalVal] = useState<T>(() => {
@@ -42,6 +44,7 @@ function useLocalStorage<T>(key?: string, initialValue?: T) {
const setVal = (localKey, value: T | ((val: T) => T)) => {
console.log(localKey, value);
try {
setUpdate(update + 1);
// Same API as useState meaning we can accept a function for value
const valForStore = value instanceof Function ? value(localVal) : value;
// Save that state so we repli useState