Skip to content

Commit 1dd39c4

Browse files
authored
fix: useURLQueryState parse the first key in search uri with question mark (#205)
* fix: `useURLQueryState` parse the first key in search uri with question mark * tweak: use URLSearchParams instead of qs
1 parent 52ac2cf commit 1dd39c4

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

packages/uikit/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
"google-libphonenumber": "^3.2.30",
6767
"lodash-es": "^4.17.21",
6868
"mantine-react-table": "0.9.5",
69-
"qs": "^6.11.2",
7069
"rc-tree": "^5.8.2",
7170
"react-hook-form": "6.15.8",
7271
"react-phone-input-2": "^2.15.1",
@@ -84,7 +83,6 @@
8483
"@types/dompurify": "^2.3.4",
8584
"@types/google-libphonenumber": "^7.4.23",
8685
"@types/lodash-es": "^4.17.8",
87-
"@types/qs": "^6.9.11",
8886
"@types/react": "^18.2.48",
8987
"@types/react-dom": "^18.2.18",
9088
"@types/react-table": "^7.7.12",
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import qs from 'qs'
21
import { useState } from 'react'
32

43
type TQueryState = { [key: string]: any }
@@ -7,17 +6,20 @@ export function useURLQueryState(
76
key: string,
87
defaultValue?: TQueryState
98
): [TQueryState | undefined, (value: TQueryState) => void] {
10-
const parsed = qs.parse(window.location.search)
9+
const parsed = parse(window.location.search)
1110
const initialValue = parsed[key] ? JSON.parse(parsed[key] as string) : defaultValue
1211
const [formState, setFormState] = useState(initialValue)
1312
const value = formState
1413
const setValue = (partialValue: Partial<TQueryState>) => {
1514
const newFormState = { ...value, ...partialValue }
1615
setFormState(newFormState)
17-
const curSearch = qs.parse(window.location.search)
16+
const curSearch = parse(window.location.search)
1817
curSearch[key] = JSON.stringify(newFormState)
19-
window.history.replaceState({}, document.title, `?${qs.stringify(curSearch).substring(3)}`)
18+
const searchParams = new URLSearchParams(curSearch)
19+
window.history.replaceState({}, document.title, `?${searchParams.toString()}`)
2020
}
2121

2222
return [value, setValue]
2323
}
24+
25+
const parse = (search: string) => Object.fromEntries(new URLSearchParams(search).entries())

pnpm-lock.yaml

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)