Skip to content

Commit

Permalink
fix(useUrlSearchParams): should return initialValue (#1583)
Browse files Browse the repository at this point in the history
  • Loading branch information
odex21 committed May 16, 2022
1 parent 356da7d commit fe58f8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/core/useUrlSearchParams/index.test.ts
Expand Up @@ -46,6 +46,16 @@ describe('useUrlSearchParams', () => {
expect(params.foo).toBe('bar')
})

test('return initialValue', async () => {
const params = useUrlSearchParams(mode, {
initialValue: {
foo: 'bar',
},
})

expect(params.foo).toBe('bar')
})

test('update params on poststate event', async () => {
const params = useUrlSearchParams(mode)
expect(params.foo).toBeUndefined()
Expand Down
8 changes: 6 additions & 2 deletions packages/core/useUrlSearchParams/index.ts
Expand Up @@ -44,7 +44,7 @@ export function useUrlSearchParams<T extends Record<string, any> = UrlParams>(
if (!window)
return reactive(initialValue) as T

const state: Record<string, any> = reactive(initialValue)
const state: Record<string, any> = reactive({})

function getRawParams() {
if (mode === 'history') {
Expand Down Expand Up @@ -129,7 +129,11 @@ export function useUrlSearchParams<T extends Record<string, any> = UrlParams>(
if (mode !== 'history')
useEventListener(window, 'hashchange', onChanged, false)

updateState(read())
const initial = read()
if (initial.keys().next().value)
updateState(initial)
else

This comment has been minimized.

Copy link
@arcs-

arcs- Jun 23, 2022

Contributor

Why do we ignore initialValue when we have params in the url? Shouldn't we combine the initial read state and the initialValue?

Object.assign(state, initialValue)

return state as T
}

0 comments on commit fe58f8a

Please sign in to comment.