Skip to content

Commit 561d57c

Browse files
TkDodoI-3B
authored andcommitted
fix: make sure the state-change effect runs for multi parsers (47ng#1163)
1 parent 5f1462d commit 561d57c

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

packages/e2e/shared/specs/native-array.cy.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,20 @@ export const testNativeArray = createTest('parseAsNativeArray', ({ path }) => {
1717
cy.get('#add-button').click()
1818
cy.location('search').should('eq', '?test=1&test=2')
1919
})
20+
it('works with the browser back button', () => {
21+
cy.visit(path)
22+
cy.contains('#hydration-marker', 'hydrated').should('be.hidden')
23+
cy.get('#client-name').should('be.empty')
24+
cy.get('#add-button').click()
25+
cy.get('#add-button').click()
26+
cy.get('#add-button').click()
27+
cy.location('search').should('eq', '?test=1&test=2&test=3')
28+
cy.get('#client-name').should('have.text', '1 - 2 - 3')
29+
cy.go('back')
30+
cy.location('search').should('eq', '?test=1&test=2')
31+
cy.get('#client-name').should('have.text', '1 - 2')
32+
cy.go('forward')
33+
cy.location('search').should('eq', '?test=1&test=2&test=3')
34+
cy.get('#client-name').should('have.text', '1 - 2 - 3')
35+
})
2036
})

packages/e2e/shared/specs/native-array.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import { parseAsInteger, parseAsNativeArrayOf, useQueryState } from 'nuqs'
44
import { Display } from '../components/display'
55

6-
export const parser = parseAsNativeArrayOf(parseAsInteger)
6+
export const parser = parseAsNativeArrayOf(parseAsInteger).withOptions({
7+
history: 'push'
8+
})
79

810
export function NativeArray() {
911
const [state, setState] = useQueryState('test', parser)

packages/nuqs/src/useQueryStates.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,15 @@ export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
153153
setInternalState(state)
154154
}
155155
queryRef.current = Object.fromEntries(
156-
Object.values(resolvedUrlKeys).map(urlKey => [
157-
urlKey,
158-
initialSearchParams?.get(urlKey) ?? null
159-
])
156+
Object.entries(resolvedUrlKeys).map(([key, urlKey]) => {
157+
const parser = keyMap[key]
158+
return [
159+
urlKey,
160+
parser?.type === 'multi'
161+
? initialSearchParams?.getAll(urlKey)
162+
: (initialSearchParams?.get(urlKey) ?? null)
163+
]
164+
})
160165
)
161166
}
162167

@@ -182,7 +187,7 @@ export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
182187
}
183188
}, [
184189
Object.values(resolvedUrlKeys)
185-
.map(key => `${key}=${initialSearchParams?.get(key)}`)
190+
.map(key => `${key}=${initialSearchParams?.getAll(key)}`)
186191
.join('&'),
187192
JSON.stringify(queuedQueries)
188193
])

0 commit comments

Comments
 (0)