From 8dc340c20283ab12102e896fe2a57c90cab27d3f Mon Sep 17 00:00:00 2001 From: Sam Hoque <30783651+SamHoque@users.noreply.github.com> Date: Wed, 29 May 2024 07:23:02 +0700 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Added=20a=20loading=20stat?= =?UTF-8?q?e=20to=20the=20storage=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hook.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 () => {