import { computed, ref } from "vue" import { useQuery, keepPreviousData } from "@tanstack/vue-query" const props = defineProps<{ userId: number|null }>() // enabled: skip query when userId is null const { data:user } = useQuery({ queryKey: computed(()=>["user",props.userId]), queryFn: ()=>fetchUser(props.userId!), enabled: computed(()=>props.userId!==null), }) // select: transform result; re-render only when selected slice changes const { data:userName } = useQuery({ queryKey: ["user",1], queryFn: ()=>fetchUser(1), select: d=>d.name, }) // refetchInterval: poll every 5 s const { data:price } = useQuery({ queryKey: ["stock","AAPL"], queryFn: ()=>fetchStockPrice("AAPL"), refetchInterval: 5000, refetchIntervalInBackground: false, }) // placeholderData: show previous page while next loads const page = ref(1) const { data:posts } = useQuery({ queryKey: computed(()=>["posts",page.value]), queryFn: ()=>fetchPosts(page.value), placeholderData: keepPreviousData, }) Skips the query entirely — no fetch triggered until enabled becomes true"> import { computed, ref } from "vue" import { useQuery, keepPreviousData } from "@tanstack/vue-query" const props = defineProps<{ userId: number|null }>() // enabled: skip query when userId is null const { data:user } = useQuery({ queryKey: computed(()=>["user",props.userId]), queryFn: ()=>fetchUser(props.userId!), enabled: computed(()=>props.userId!==null), }) // select: transform result; re-render only when selected slice changes const { data:userName } = useQuery({ queryKey: ["user",1], queryFn: ()=>fetchUser(1), select: d=>d.name, }) // refetchInterval: poll every 5 s const { data:price } = useQuery({ queryKey: ["stock","AAPL"], queryFn: ()=>fetchStockPrice("AAPL"), refetchInterval: 5000, refetchIntervalInBackground: false, }) // placeholderData: show previous page while next loads const page = ref(1) const { data:posts } = useQuery({ queryKey: computed(()=>["posts",page.value]), queryFn: ()=>fetchPosts(page.value), placeholderData: keepPreviousData, }) Skips the query entirely — no fetch triggered until enabled becomes true" />

Prev Next

Web / TanStack Interview Questions

What are the most useful useQuery options beyond queryKey and queryFn in Vue?

TanStack Query exposes many options; the following solve the most common real-world patterns.

<script setup lang="ts">
import { computed, ref } from "vue"
import { useQuery, keepPreviousData } from "@tanstack/vue-query"

const props = defineProps<{ userId: number|null }>()

// enabled: skip query when userId is null
const { data:user } = useQuery({
  queryKey: computed(()=>["user",props.userId]),
  queryFn:  ()=>fetchUser(props.userId!),
  enabled:  computed(()=>props.userId!==null),
})

// select: transform result; re-render only when selected slice changes
const { data:userName } = useQuery({
  queryKey: ["user",1],
  queryFn:  ()=>fetchUser(1),
  select:   d=>d.name,
})

// refetchInterval: poll every 5 s
const { data:price } = useQuery({
  queryKey: ["stock","AAPL"],
  queryFn:  ()=>fetchStockPrice("AAPL"),
  refetchInterval: 5000,
  refetchIntervalInBackground: false,
})

// placeholderData: show previous page while next loads
const page = ref(1)
const { data:posts } = useQuery({
  queryKey: computed(()=>["posts",page.value]),
  queryFn:  ()=>fetchPosts(page.value),
  placeholderData: keepPreviousData,
})
</script>
What does the select option do?
What does enabled:false on a useQuery do?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is TanStack and what problem does it solve for frontend developers? What is TanStack Query and what core problems does it solve over plain fetch in Vue? How do you set up TanStack Query in a Vue application? How should you design queryKeys in TanStack Query and why do they matter? What is the difference between staleTime and gcTime in TanStack Query? What are the most useful useQuery options beyond queryKey and queryFn in Vue? What is useMutation and how do you use it to submit data in Vue? How do you write dependent (sequential) queries in TanStack Query with Vue? How do you implement pagination and infinite scroll with TanStack Query? How do you prefetch data in TanStack Query to speed up navigation? What is TanStack Router and how does it differ from Vue Router? How do you define routes in TanStack Router with Vue? How do you use URL search params as typed state in TanStack Router? What are route loaders in TanStack Router and how do they integrate with TanStack Query? What is TanStack Table and how do you create a basic table in Vue? How do you add client-side sorting and column filtering to a TanStack Table in Vue? How do you implement pagination with TanStack Table? How do you add column visibility toggling and row selection to a TanStack Table? What is TanStack Form and how do you build a basic form in Vue? How do you add synchronous and asynchronous field validation in TanStack Form? How do you manage dynamic field arrays in TanStack Form? What is TanStack Virtual and when should you use it? How do you handle variable-height rows in TanStack Virtual? How do you combine useInfiniteQuery with useVirtualizer for an infinitely scrolling virtualised list? How do you implement optimistic updates with TanStack Query? What are the status and fetchStatus values in TanStack Query v5 and how do they drive UI? How do you create links and perform programmatic navigation in TanStack Router with Vue? How does TanStack Router handle route-level errors and loading states? How do you integrate TanStack Table with server-side sorting, filtering and pagination? What developer tools does TanStack provide and how do you add them in Vue? What are query key factories and why are they recommended for large Vue apps? How do you implement cross-field (form-level) validation and handle submission in TanStack Form? How do you pass variables to a mutation and use them across lifecycle callbacks? How do you virtualise a horizontal list or two-dimensional grid with TanStack Virtual? What are the most common TanStack Query mistakes in Vue and how do you avoid them? How do you implement protected routes and auth redirects in TanStack Router? How do you render custom Vue components inside TanStack Table cells? How do the TanStack libraries compose into a full application stack, and what is TanStack Start?
Show more question and Answers...

DataStructures

Comments & Discussions