-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Closed
Description
Describe the bug
When updating a ref created with useRouteQuery, trigger is called twice:
- once during the
setmethod (https://github.com/vueuse/vueuse/blob/main/packages/router/useRouteQuery/index.ts#L68) - again during the
watchcallback on the route (https://github.com/vueuse/vueuse/blob/main/packages/router/useRouteQuery/index.ts#L94)
This doesn't appear to trigger watchers twice if watching the ref directly. This may be because vue isn't calling watchers if an equality check passes (old value === new value).
This is an issue if you create a computed with a non-scalar value using the value from the ref, like:
const foo = useRouteQuery('foo');
const bar = computed(() => ({ bar })); // gets re-created twice whenever foo is updatedIn this case watchers will be called twice because the object created by the computed will not pass an equality check, even if all the inner values are the same.
This is also presumably an issue with useRouteParams and useRouteHash with both take a similar approach.
I will try to create a PR to fix this by applying the same check inside the watcher that's used in the setter:
watch(
() => route.query[name],
(v) => {
// add the same equality check to prevent unnecessary trigger
if (query === v)
return
query = v
_trigger()
},
{ flush: 'sync' },
)Reproduction
System Info
System:
OS: Linux 6.8 Ubuntu 24.04 LTS 24.04 LTS (Noble Numbat)
CPU: (20) x64 13th Gen Intel(R) Core(TM) i7-13700H
Memory: 9.01 GB / 31.02 GB
Container: Yes
Shell: 5.9 - /usr/bin/zsh
Binaries:
Node: 20.13.1 - ~/.nvm/versions/node/v20.13.1/bin/node
npm: 10.5.2 - ~/.nvm/versions/node/v20.13.1/bin/npm
Browsers:
Chrome: 126.0.6478.126
npmPackages:
@vueuse/core: ^10.11.0 => 10.11.0
@vueuse/router: ^10.11.0 => 10.11.0
vue: ^3.4.27 => 3.4.31Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a VueUse issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion.
- The provided reproduction is a minimal reproducible example of the bug.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels