-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
useRouteQuery
can only custom deserialize but cannot custom serialize
#3970
Labels
enhancement
New feature or request
Comments
matthew-dean
changed the title
May 14, 2024
useRouteQuery
can only deserialize but cannot serializeuseRouteQuery
can only custom deserialize but cannot custom serialize
Okay, I did think of a workaround right after: import { useRouteQuery } from '@vueuse/router'
import { dayjs } from './date'
import { computed } from 'vue'
export function useQueryDate(name: string, defaultValue?: Date) {
const query = useRouteQuery<string | undefined>(
name,
defaultValue ? dayjs(defaultValue).format('YYYY-MM-DD') : undefined
)
return computed({
get() {
return query.value ? dayjs(query.value).toDate() : undefined
},
set(value: Date | undefined) {
query.value = value ? dayjs(value).format('YYYY-MM-DD') : undefined
}
})
} |
4 tasks
Custom serializers already exist in other utilities, such as const create = useRouteQuery<boolean>("create", false, {
serializer: {
read: (v: string) => v === null,
write: (v: boolean) => v ? null : undefined
}
})
create.value = false; // /
create.value = true; // /?create |
|
Isn't this a duplicate of #3929? |
5 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clear and concise description of the problem
I stumbled into this because I expected that for sure
useRouteQuery
would allow deserialization AND serialization transforms in order to store any arbitrary state in the query string.In my case, I had local refs for
startDate
andendDate
as JavaScript dates. I decided I wanted to persist these dates into the query string. The thing is,useRouteQuery
only allows one-way transforms. That is, if I wanted to store the default value as aYYYY-MM-DD
, and then deserialize toDate
, that seems fine. However, this utility is extremely limited, because I could not control serializing the date BACK into the querystring. (It just defaults to JavaScript's crap date serialization i.e.?startDate=Mon+Apr+15+2024+14:29:33+GMT-0700+(Pacific+Daylight+Time)
)Suggested solution
Allow two-way transforms i.e. serialization. I'd suggest something like:
Alternative
I don't see any alternatives immediately, other than using a different library or a drastically different approach.
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: