I am wondering how useState() works in a serverless environment. How can local state be saved if different servers might be serving different page refreshes?
I am specifically asking in the context of optimistic concurrency. Basically I want to allow users to star a post. What I have right now does something like:
User clicks ‘star post’ button.
A request to the database is made to update the state and ensure that the post is starred.
I add a reload() directive to force a page refresh.
On page refresh a call to the db is made which reads the updated state and displays the right value.
This however is slow, since we do all these db queries.
What is the alternative exactly, in a serverless environment? I have had people suggest TanStack / ReactQuery, but I don’t understand how they would work in a serverless setup. On page refresh a different server might be answering the user query, so how would I use the local state to optimistically display the post as starred before the db has replies?
So the essence of my question is – what happens in detail when using useState or other such hooks which require state management?
I have tried to run db queries, but that’s too slow. Using local state makes sense, but. I am not sure how I could make that happen in a serverless environment.
Heremit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.