How to fetch data on-demand in TanStack Query based on dynamic parameters (e.g. name from a list)
I’m using TanStack Query and have a situation where I first fetch a list of names and render them in a tree component. When I click on a name, I want to expand it and then lazily fetch additional data specific to that name.
Since I don’t know the names in advance, I can’t predefine queries with
enabled: false
and specific parameters. So my question is: How can I trigger a query with a specific parameter (e.g. a name) only on button click, without pre-initializing it?
If this isn’t possible directly with TanStack Query, what would be the best practice to handle this type of use case?2 Replies
eastern-cyan•2mo ago
Disabling/Pausing Queries | TanStack Query Vue Docs
If you ever want to disable a query from automatically running, you can use the enabled = false option. The enabled option also accepts a callback that returns a boolean. When enabled is false: If the...
eastern-cyan•2mo ago
Or just conditionally render a component