Reputation: 11809
After logging into an app (React.js), I am caching the member
data in localStorage as a lot of my components are using it and request only needs to be done upon log-in, ideally.
However, a few properties in this member
object may be changed in the backend manually so the frontend doesn't have a way to know whether the member
object has changed at all. (Again, ideally, any change to the member
object should go through some form submission that directly changes the DB, with which an update can be triggered for the localStorage, but this is not an option at this time.)
Example scenario: There's a generic form in the app to request for additional credits. Customer service will receive an email regarding the request. Credits would be manually updated for Customer A (in DB). If Customer A doesn't re-login (where the get request for member
is done), localStorage will still show the old no. of credits.
If this is the situation, what's the best way to go about it?
Don't store member
data in localStorage at all so as to keep the data fresh. Just call the endpoint whenever it's needed.
Use sessionStorage
instead?
Trigger a refetch when the user refreshes the page / app (although user may not know that they need to do this to update the data).
Suggestions?
Upvotes: 1
Views: 2035
Reputation: 188
If you do not know when member
has been updated, don't store it. You should query the back end every time you need member
. That is the only way to keep the data sync with your database.
Upvotes: 0
Reputation: 78890
At a high level, you have two choices:
The latter option would require a lot of changes, and it changes the scalability of your app, so the former choice seems like the most reasonable option for you.
It's smart to keep using localStorage
so you have an offline copy of the data and aren't blocking rendering during page load; you can have a background periodic refresh process that doesn't disrupt the user in the meantime. If your data is mirrored in something like redux or context, then your UI could seemlessly update if/when the data changes.
Upvotes: 1
Reputation: 2313
I'd go with just getting the data fresh.
Upvotes: 1