Next.js Discord

Discord Forum

How to cache the result of an call made in a hook?

Unanswered
Giant resin bee posted this in #help-forum
Open in Discord
Giant resin beeOP
I have an app which displays the temperature of a famous big city at different places in the app, in different modules that are all on-screen.
For this I have an useTemperature() that is called in all those modules and as a result of react re-rendering, etc, is called many many times in a row and I would like to avoid making that many API calls.
I have tried some custom logic but it didn't work as there is concurrency to access the useTemperature() hook so I get many simultaneous calls.
What is the standard solution for a case like this one?

2 Replies

Asian paper wasp
TL;DR TanStack Query, with staleTime set to a certain duration, such that the API response cache will be used
aka React Query