How can we use a computed property in the query key (e.g. page number)?
See original GitHub issueThe react-query docs describe how to use a compound query key (Array Keys). I want to be able to do something similar with vue-query, but I’ve struggled to make it work.
💡 Proposed Vue 3 Composition API Example:
export default defineComponent({
// ...
setup: (props) => {
const queryKey = `widgets:${props.query}`;
const page = ref(0);
const { data: widgets, isFetching: fetching } = useQuery<Widget[]>(
reactive([queryKey, computed(() => page.value)],
() =>
getWidgets({
p: page.value,
q: props.query,
})
)
})
… but that doesn’t work as-is. Changing the page value does not trigger a re-query. The computed
does’t appear to do anything; and otherwise I need to explicitly trigger an invalidation (e.g. queryClient.invalidateQueries([queryKey, 0]);
).
Is there some other method I’m missing for how to use a reactive value in the QueryKey?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Computed Properties - Vue.js
You can data-bind to computed properties in templates just like a normal property. Vue is aware that this.publishedBooksMessage depends on this.author.books , ...
Read more >Is it possible to use the computed properties ... - Stack Overflow
You need to use correct scope to access a vue computed property. as you are using just id , it will search it...
Read more >Understanding computed properties in Vue.js - LogRocket Blog
You can use computed properties to calculate and display values based on a value or set of values in the data model.
Read more >Query Parameters - Routing - Ember Guides
Common use cases for query params include representing the current page number in a paginated collection, filter criteria, or sorting criteria.
Read more >Entity Property Reference - App Engine - Google Cloud
This page describes how to use the legacy bundled services and APIs. ... When a query involves a property with values of mixed...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@andresespinosapc Unfortunately Vue reactivity cannot unwrap a ref directly in the array as mentioned in official Vue docs: https://v3.vuejs.org/guide/reactivity-fundamentals.html#access-in-reactive-objects
Second relevant piece of information is that Vue cannot track plain object changes, and uses ES6 proxies for that. If you unwrap it by yourself, Vue will loose abili to track changes for it. https://v3.vuejs.org/guide/reactivity.html#how-vue-tracks-these-changes
I think you should be able to use computed the same way as ref. So inside an object of reactive array.
Hi @bikegriffith, Reactivity in Vue is… very hard to nail down. It has some quirks that you have to be aware of.
You almost nailed it down. If you take a look at the third code block from the Readme, you will notice that ref is passed to the object, not directly to the query key array.
So in your case, something like this should work:
Let me know if that works for you.