PieterP
tRPC3y ago
6 replies
Pieter

Query function depends on a variable

In tRPC v10 accessing a specific path is really easy, but because of that I don't control the query key.

I'm using tRPC to fetch data based on user input in a search bar. Tanstack Query docs has a specific bit in the detailing that the variable should be included in the query key. What is the tRPC way of handeling this?
https://tanstack.com/query/latest/docs/react/guides/query-keys#if-your-query-function-depends-on-a-variable-include-it-in-your-query-key

Yes, I can use refetch with the query params in state but this is creating side-effects in my code that I would like to avoid.
At its core, TanStack Query manages query caching for you based on query keys. Query keys have to be an Array at the top level, and can be as simple as an Array with a single string, or as complex as an array of many strings and nested objects. As long as the query key is serializable, and unique to the query's data, you can use it!

Simple Que...
Query Keys | TanStack Query Docs
Was this page helpful?