Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useStorage): fix defaults not unwrapped #3534

Merged
merged 3 commits into from Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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