Skip to content

Commit

Permalink
fix(useStorageValue): Allow setting of state even if the component ha…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
ArttuOll committed Oct 9, 2022
1 parent 473be57 commit 28e73ad
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/useStorageValue/useStorageValue.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -157,7 +156,7 @@ export function useStorageValue<T>(
});

const isFirstMount = useFirstMountState();
const [state, setState] = useSafeState<T | null | undefined>(
const [state, setState] = useState<T | null | undefined>(
initializeWithStorageValue && isFirstMount ? (methods.current.fetchVal() as T) : undefined
);
const prevState = usePrevious(state);
Expand Down

0 comments on commit 28e73ad

Please sign in to comment.