-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
URL state not available via getters #400
Comments
Thanks for the detailed report. I'll have a look later, but at first glance, it looks like your initialisation performs side-effects in the render function, which is not recommended by React. There are a few other things to consider:
|
This comment was marked as outdated.
This comment was marked as outdated.
@franky47 I've found a weird behavior. I won't go into details until you say it's a real issue. The case:
const useMenuQueryState = () =>
useQueryState('menu', parseAsBoolean.withDefault(false)); like this: console.log(
new URLSearchParams(Array.from(searchParams.entries())).toString()
) The logged query doesn't change even though I see the change being applied by your library in the address bar. Questions:
new URLSearchParams(Array.from(searchParams.entries())).toString(), Thanks! |
@damian-balas in stock1 Next.js, Edit: if you want to be notified of URL updates, you can use the import { subscribeToQueryUpdates } from 'next-usequerystate'
React.useLayoutEffect(
() => subscribeToQueryUpdates(({ search }) => console.log(search.toString()),
[]
) Footnotes
|
Thanks :) const useSubscribedSearchParams = () => {
const _searchParams = useSearchParams();
const [searchParams, setSearchParams] =
useState<URLSearchParams>(_searchParams);
useLayoutEffect(
() =>
subscribeToQueryUpdates(({ search }) => {
setSearchParams(search);
}),
// * This returns an unsubscribe function
[],
);
return searchParams;
}; It works well :) My last question is... how should I actually handle changing dynamic queries? Example:
How would you handle that case? Help much appreciated! :) |
I would use a useQueryState to update the URL, that's what it's intended to do. If the key is not known ahead of time, or there can be an arbitrary number of them, you can use function DynamicQuery({ keys }: { keys: string[] }) {
const [values, update] = useQueryStates(
// Note: might want to memoize this
keys.reduce((obj, key) => ({
...obj,
[key]: parseAsString
}), {})
) |
Thanks for the example, but how can I type this properly, since values in this case is of type {} and using |
@oliverstreissi this should do it (note however that this is a bit of a hack): import { Parser, parseAsString, useQueryStates } from 'nuqs'
const [values, update] = useQueryStates(
// Note: might want to memoize this
keys.reduce(
(obj, key) => ({
...obj,
[key]: parseAsString
}),
{} as Record<string, Parser<string>>
)
) If using default values, you'll need to be explicit about the default value existing on the const [values, update] = useQueryStates(
// Note: might want to memoize this
keys.reduce(
(obj, key) => ({
...obj,
[key]: parseAsString.withDefault('')
}),
{} as Record<string, Parser<string> & { defaultValue: string }>
)
) |
@damian-balas I am under the impression that this is solved so I will close it. I can reopen if needed, of course. |
This ticket is now about #400 (comment) by damian-balas.
(I moved my report to #404.)
The text was updated successfully, but these errors were encountered: