Skip to content

Commit

Permalink
fix(useStorage): fix undefined defaults (#3597)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doctor-wu committed Dec 4, 2023
1 parent bbd2fad commit 0422078
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/core/useStorage/index.test.ts
Expand Up @@ -113,6 +113,17 @@ describe('useStorage', () => {
expect(storedValue).toBeFalsy()
})

it('undefined value', () => {
storage.removeItem(KEY)

const store = useStorage(KEY, undefined, storage)
const storedValue = storage.getItem(KEY)

expect(store.value).toBe(undefined)
expect(storage.getItem(KEY)).toBe(undefined)
expect(storedValue).toBeFalsy()
})

it('remove value', async () => {
storage.setItem(KEY, 'random')

Expand Down
2 changes: 1 addition & 1 deletion packages/core/useStorage/index.ts
Expand Up @@ -231,7 +231,7 @@ export function useStorage<T extends(string | number | boolean | object | null)>
: storage!.getItem(key)

if (rawValue == null) {
if (writeDefaults && rawInit !== null)
if (writeDefaults && rawInit != null)
storage!.setItem(key, serializer.write(rawInit))
return rawInit
}
Expand Down

0 comments on commit 0422078

Please sign in to comment.