diff --git a/src/hook.ts b/src/hook.ts index 7911fba..f53037c 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -34,6 +34,7 @@ export function useStorage( readonly setRenderValue: React.Dispatch> readonly setStoreValue: (v: T) => Promise readonly remove: () => void + readonly isLoading: boolean } ] export function useStorage( @@ -45,6 +46,7 @@ export function useStorage( readonly setRenderValue: React.Dispatch> readonly setStoreValue: (v?: T) => Promise readonly remove: () => void + readonly isLoading: boolean } ] export function useStorage(rawKey: RawKey, onInit?: Setter) { @@ -54,7 +56,8 @@ export function useStorage(rawKey: RawKey, onInit?: Setter) { // Render state const [renderValue, setRenderValue] = useState(onInit) - + const [isLoading, setIsLoading] = useState(true) + // Use to ensure we don't set render state after unmounted const isMounted = useRef(false) @@ -95,6 +98,7 @@ export function useStorage(rawKey: RawKey, onInit?: Setter) { [key]: (change) => { if (isMounted.current) { setRenderValue(change.newValue) + setIsLoading(false) } } } @@ -110,6 +114,7 @@ export function useStorage(rawKey: RawKey, onInit?: Setter) { } else { setRenderValue(v !== undefined ? v : onInit) } + setIsLoading(false) }) return () => {