Next.js Discord

Discord Forum

How do you persist data from a fetch and only fetch again if the data changed?

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Avatar
Transvaal lionOP
I'm not super sure what would be the best approach for this.

I have a NextAuth application that fetches an access token and I then use the access token to access data from an API. What would be the more ideal way to persist data from the API, so I don't need to do unnecessary fetches and store it locally on the user's client? Do I store it on next-auth? Do I store it on localStorage?

I would like to fetch it one time and check if there's any sort of data already in the client, then there's no need to fetch again. How would I approach this problem?
Answered by Cape horse mackerel
Install @tanstack/react-query and hit it
View full answer

13 Replies

Avatar
Cape horse mackerel
Install @tanstack/react-query and hit it
Answer
Avatar
Transvaal lionOP
Is this the conventional way to approach this? How did people cache the data before this package?
Avatar
most people store the cache in redux state before react-query exists
Avatar
Transvaal lionOP
Ah, I see! What about localStorage? Is this used for something else? I'm trying to see what was the approach used with just HTML, CSS and JavaScript and I'll be done with this thread.
Avatar
if you store in localstorage, how do you know if the data has changed?
Avatar
Transvaal lionOP
Makes sense. What was used pre-React, just HTML, CSS and JavaScrit?
Avatar
for what? for building a website?
what are you trying to build? why would you want to use old tools lol
Avatar
Transvaal lionOP
Yes! For building a web application.
I need to understand it. Point me in the right direction and I'll read on it.
Avatar
jquery was very popular
php + jquery
Avatar
Transvaal lionOP
Perfect! Thank you!