Do parsers support async values? #1255
-
|
Hi! I have a question about how Consider the case where I receive some value from the URL (e.g. const data = useAsyncData();
const values = data ?? [];
const [state, setState] = useQueryState(
"service",
parseAsStringEnum(values).withDefault("")
);In such situations the received state always represents the default value. It seems that the workaround is to use a non-validating parser initially and perform the validation myself once the async data becomes available: const data = useAsyncData();
const values = data ?? [];
const [state, setState] = useQueryState(
"service",
parseAsString.withDefault("")
);
const validState = data.includes(state) ? state : "";Is it the preferred approach or am I missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
What should happen if a user clicked on a link that said One way would be to feed the promise to Parsers should be pure functions, side-effect free, and synchronous. Moreover, all useQueryState(s) hooks should have the same parser for a given key, see https://nuqs.dev/docs/troubleshooting#different-parsers-on-the-same-key. |
Beta Was this translation helpful? Give feedback.
What should happen if a user clicked on a link that said
?service=swap, before the set of valid values is known? Or what should happen if they click on an invalid link?service=banana?One way would be to feed the promise to
useand let the component suspend while it's pending, then you'd get the set of values to feed to the parser.Parsers should be pure functions, side-effect free, and synchronous. Moreover, all useQueryState(s) hooks should have the same parser for a given key, see https://nuqs.dev/docs/troubleshooting#different-parsers-on-the-same-key.