question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can we use a computed property in the query key (e.g. page number)?

See original GitHub issue

The 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DamianOsipiukcommented, Sep 4, 2021

@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

Ref unwrapping only happens when nested inside a reactive Object. There is no unwrapping performed when the ref is accessed from an Array or a native collection type like Map

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.

1reaction
DamianOsipiukcommented, Jul 7, 2021

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:

export default defineComponent({
  // ...
  setup: (props) => {
    const queryKey = `widgets:${props.query}`;
    const page = ref(0);
    const { data: widgets, isFetching: fetching } = useQuery<Widget[]>(
      reactive([queryKey, {page}]),
      () =>
        getWidgets({
          p: page.value,
          q: props.query,
        })
    )
})

Let me know if that works for you.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found