How do you persist data from a fetch and only fetch again if the data changed?
Answered
Transvaal lion posted this in #help-forum
Transvaal lionOP
I'm not super sure what would be the best approach for this.
I have a
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?
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?
13 Replies
Cape horse mackerel
Install
@tanstack/react-query
and hit itAnswer
Transvaal lionOP
Is this the conventional way to approach this? How did people cache the data before this package?
most people store the cache in redux state before react-query exists
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.if you store in localstorage, how do you know if the data has changed?
Transvaal lionOP
Makes sense. What was used pre-React, just HTML, CSS and JavaScrit?
for what? for building a website?
what are you trying to build? why would you want to use old tools lol
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.
jquery was very popular
php + jquery
Transvaal lionOP
Perfect! Thank you!