Filtering Data in React Query Like a Pro | by Juan Preciado | Feb, 2022

Downside assertion — Let’s think about an API returning the identical payload because the part above, however this time, the API doesn’t permit filtering, so it must be carried out within the consumer, we would like additionally to reduce API requests.

The answer, selectors — are a not so well-known however very highly effective device in React Question, they permit to filter and/or remodel the question consequence with out touching the cached knowledge.

So we are able to join the selector to your question hook, and carry out the filtering there primarily based on the filters you choose in your element:

Discover how we’re passing a 3rd argument to the `useQuery` operate, this object can include a set of useQuery . On this case, we’re passing a selector operate through the choose property.

On this choose operate, we’ll do any filtering or normalization our element wants, adjustments within the filtering is not going to set off any API requests

More Posts