Skip to content

Commit

Permalink
fix(useStorage): fix defaults not unwrapped (#3534)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue committed Nov 9, 2023
1 parent 000337e commit b6d2bd3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/core/useStorage/index.test.ts
Expand Up @@ -467,4 +467,19 @@ describe('useStorage', () => {
ref.value = 1
expect(console.error).toHaveBeenCalledWith(new Error('write item error'))
})

it.each([
1,
'a',
[1, 2],
{ a: 1 },
new Map([[1, 2]]),
new Set([1, 2]),
])('should work in conjunction with defaults', (value) => {
const basicRef = useStorage(KEY, () => value, storage)
expect(basicRef.value).toEqual(value)
storage.removeItem(KEY)
const objectRef = useStorage(KEY, value, storage)
expect(objectRef.value).toEqual(value)
})
})
2 changes: 1 addition & 1 deletion packages/core/useStorage/index.ts
Expand Up @@ -145,7 +145,7 @@ export function useStorage<T extends(string | number | boolean | object | null)>
},
} = options

const data = (shallow ? shallowRef : ref)(defaults) as RemovableRef<T>
const data = (shallow ? shallowRef : ref)(typeof defaults === 'function' ? defaults() : defaults) as RemovableRef<T>

if (!storage) {
try {
Expand Down

0 comments on commit b6d2bd3

Please sign in to comment.