javascript - User Session control react js - Stack Overflow

admin2025-04-16  3

I have a question-answer web app. When the user clicks on 'knee' in my app, some questions about the knee are fetched from the backend. The user can answer these questions, and once all the questions are answered and submitted, the answers are sent to the backend, which then responds.

Now, consider this scenario carefully: If the user answers five questions but their device shuts down unexpectedly, when they reopen the browser, they should be able to see the previously answered questions.

How can I handle this situation so that the user's data is retained even if their device shuts down? Also, I don't want to send a request to the backend on every answer, as it would increase costs. What is the best way to handle this?"

Would you like me to improve it further or simplify it?

I have a question-answer web app. When the user clicks on 'knee' in my app, some questions about the knee are fetched from the backend. The user can answer these questions, and once all the questions are answered and submitted, the answers are sent to the backend, which then responds.

Now, consider this scenario carefully: If the user answers five questions but their device shuts down unexpectedly, when they reopen the browser, they should be able to see the previously answered questions.

How can I handle this situation so that the user's data is retained even if their device shuts down? Also, I don't want to send a request to the backend on every answer, as it would increase costs. What is the best way to handle this?"

Would you like me to improve it further or simplify it?

Share Improve this question asked Feb 4 at 8:35 RameshRamesh 293 bronze badges 2
  • The easiest is to make sure that (if you're using a cookie to store the user's session identifier) it's a permanent cookie (not a session cookie). – AKX Commented Feb 4 at 8:44
  • localStorage + onChange – Shuo Commented Feb 4 at 8:47
Add a comment  | 

1 Answer 1

Reset to default 0

You can use localStorage to save the answers every time the user changes them. This ensures that even if the user loses their network connection or their PC shuts down, their progress will be retained.

So you can put in onChange methode a set to localStorage, and get the data from localstorage every time the application mounted.

转载请注明原文地址:http://www.anycun.com/QandA/1744733101a86850.html