From 28e73ada9a41a1e7b83301ae1f362168bb799168 Mon Sep 17 00:00:00 2001 From: Arttu Olli Date: Sat, 8 Oct 2022 16:35:23 +0300 Subject: [PATCH] fix(useStorageValue): Allow setting of state even if the component has not mounted Using normal useState to store the value of the tracked key instead of useSafeState seems to solve the problem described in #451. fix #451 --- src/useStorageValue/useStorageValue.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/useStorageValue/useStorageValue.ts b/src/useStorageValue/useStorageValue.ts index ba593b75..929b4b09 100644 --- a/src/useStorageValue/useStorageValue.ts +++ b/src/useStorageValue/useStorageValue.ts @@ -1,11 +1,10 @@ /* eslint-disable @typescript-eslint/no-use-before-define,no-use-before-define */ -import { useCallback } from 'react'; +import { useCallback, useState } from 'react'; import { useConditionalEffect } from '../useConditionalEffect/useConditionalEffect'; import { useFirstMountState } from '../useFirstMountState/useFirstMountState'; import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect/useIsomorphicLayoutEffect'; import { useMountEffect } from '../useMountEffect/useMountEffect'; import { usePrevious } from '../usePrevious/usePrevious'; -import { useSafeState } from '../useSafeState/useSafeState'; import { useSyncedRef } from '../useSyncedRef/useSyncedRef'; import { useUpdateEffect } from '../useUpdateEffect/useUpdateEffect'; import { NextState, resolveHookState } from '../util/resolveHookState'; @@ -157,7 +156,7 @@ export function useStorageValue( }); const isFirstMount = useFirstMountState(); - const [state, setState] = useSafeState( + const [state, setState] = useState( initializeWithStorageValue && isFirstMount ? (methods.current.fetchVal() as T) : undefined ); const prevState = usePrevious(state);