-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
withDefault()
behavior when reset
#531
Comments
I'm not sure I follow your idea. In the case of Setting the state to Could you show me an example on how you would use such a feature? |
@franky47 Thank you for your reply. I understood. I use |
I found this issue for the same reason described by @arwtyxouymz. Example: export const dateRanges = z.enum([
"last_month",
"month_before_last",
"current_month",
]);
const [selectValue, setSelectValue] = useQueryState(
"selected_value",
parseAsStringEnum(dateRanges.options)
);
// const selectValue: "last_month" | "month_before_last" | "current_month" | null
const [selectValueWithDefault, setSelectValueWithDefault] = useQueryState(
"selected_value_with_default",
parseAsStringEnum(dateRanges.options).withDefault("last_month")
);
// const selectValueWithDefault: "last_month" | "month_before_last" | "current_month" It would be awesome to have the option to set an initial value together with the option to set this value to null afterwards. |
@matheins The default value differs from an initial state, it's what should be returned when the URL does not provide a valid state value. If you're ok with a
Could you show me a code example on how you'd use this? |
Although my problem was resolved by adding query params to link href, // Initially we can show filtered result to users
export const useStatusFilter = () => {
const [statusFilterValue, setStatusFilterValue] = useQueryState(
'status',
parseAsStringEnum(['NotStarted', 'InProgress', 'Completed']).withInitialValue("NotStarted")
);
// filter can be cleared
const clearFilter = () => setStatus(null);
return {
statusFilterValue,
setStatusFilterValue,
clearFilter
}
} |
I think you came to the right conclusion by yourself. The reason an initial value like in |
I like the idea of @arwtyxouymz. Using |
if the value of "tab" param in URL is not match the TabType, the value of tab will be set as 'default', but the tab query in URL is still the wrong one. I don't think nuqs has this feature, so i've tried use |
It seem to be similar to my use case When I try to use parseAsIsoDateTime withDefault option, I can't clear the date since it will be fallback to the lastMonth and endOfToday. What I want it just remove these searchParams or at least set the startDate and endDate to null Whether I use clearOnDefault it still be the same or maybe I use it the wrong. please correct me! const [date, setDate] = useQueryStates(
{
startDate: searchParams.startDate,
endDate: searchParams.endDate,
page: searchParams.page,
},
{
startTransition,
shallow: false,
clearOnDefault: true,
}
)
const searchParams = {
startDate: parseAsIsoDateTime.withDefault(lastMonth),
endDate: parseAsIsoDateTime.withDefault(endOfToday),
} For more information, they are using with nuqs/server. And for now, I overwrite this pattern by remove withDefault and use the default value with useEffect on the client mount instead. const [date, setDate] = useQueryStates(
{
startDate: searchParams.startDate,
endDate: searchParams.endDate,
page: searchParams.page,
},
{
startTransition,
shallow: false,
}
)
const searchParams = {
startDate: parseAsIsoDateTime,
endDate: parseAsIsoDateTime
}
// biome-ignore lint/correctness/useExhaustiveDependencies: Set effect on mount since we only want to set the initial date
React.useEffect(() => {
setDate({
startDate: lastMonth,
endDate: endOfToday(),
})
}, []) |
Will this be implement in the future or any alternative ways? I do agree that we may need a way to clear/set the date to undefined or null whether we have default as today date or someelse |
Context
What's your version of
nuqs
?Next.js information (obtained by running
next info
):Are you using:
basePath
option in your Next.js configwindowHistorySupport
flag in your Next.js configDescription
Thank you for the great library. I love this library because it's really easy to use.
This is the kind of feature request.
When using
withDefault
, the default value is used when query value set tonull
.But I think it's useful the library has the concept similar with
initialState
ofuseState
.The initial state is set at the initial mount and it can be clear with
null
.Maybe something like this:
How do you think?
The text was updated successfully, but these errors were encountered: