-
Notifications
You must be signed in to change notification settings - Fork 183
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
Replace query-string
dependency with URLSearchParams
#336
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'playroom': patch | ||
--- | ||
|
||
Replace `query-string` dependency with `URLSearchParams` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { createBrowserHistory } from 'history'; | ||
import { useState, useEffect } from 'react'; | ||
import queryString, { type ParsedQuery } from 'query-string'; | ||
|
||
import playroomConfig from '../config'; | ||
|
||
|
@@ -11,10 +10,8 @@ export function updateUrlCode(code: string) { | |
|
||
const existingQuery = getParamsFromQuery(); | ||
|
||
const newQuery = queryString.stringify({ | ||
...existingQuery, | ||
code, | ||
}); | ||
const newQuery = new URLSearchParams(existingQuery); | ||
newQuery.set('code', code); | ||
|
||
const params = | ||
playroomConfig.paramType === 'hash' ? `#?${newQuery}` : `?${newQuery}`; | ||
|
@@ -24,18 +21,18 @@ export function updateUrlCode(code: string) { | |
|
||
export function getParamsFromQuery(location = history.location) { | ||
try { | ||
return queryString.parse( | ||
return new URLSearchParams( | ||
playroomConfig.paramType === 'hash' | ||
? location.hash.replace(/^#/, '') | ||
: location.search | ||
); | ||
} catch (err) { | ||
return {}; | ||
return new URLSearchParams(); | ||
Comment on lines
23
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
} | ||
} | ||
|
||
export function useParams<ReturnType>( | ||
selector: (rawParams: ParsedQuery) => ReturnType | ||
selector: (rawParams: URLSearchParams) => ReturnType | ||
): ReturnType { | ||
const [params, setParams] = useState(getParamsFromQuery); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
query-string
's API returned a single value or an array, butURLSearchParams
'.get
API returnsstring | null
, so there's no need for atypeof
check here anymore.