Skip to content

Commit

Permalink
fix(useStorage): isLoading becoming false before returning value (#59)
Browse files Browse the repository at this point in the history
* fix(useStorage): isLoading becoming false before returning value

* refactor(useStorage): await chain
  • Loading branch information
kirkwat authored Nov 13, 2024
1 parent 49b09da commit d876382
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,22 @@ export function useStorage<T = any>(rawKey: RawKey, onInit?: Setter<T>) {

storageRef.current.watch(watchConfig)

storageRef.current.get<T>(key)?.then((v) => {
const initializeStorage = async () => {
const storedValue = await storageRef.current.get<T>(key)

if (onInit instanceof Function) {
const initValue = onInit?.(v, true)
const initValue = onInit?.(storedValue, true)
if (initValue !== undefined) {
persistValue(initValue)
await persistValue(initValue)
}
} else {
setRenderValue(v !== undefined ? v : onInit)
setRenderValue(storedValue !== undefined ? storedValue : onInit)
}

setIsLoading(false)
})
}

initializeStorage()

return () => {
isMounted.current = false
Expand Down

0 comments on commit d876382

Please sign in to comment.