N
N
nickname133842019-10-31 09:49:22
JavaScript
nickname13384, 2019-10-31 09:49:22

Update state when data is added?

React js project.
1. There is a list of users in a separate block
2. And in another block there is a form for registering a new user
3. After registration, the user data is sent to the server
Question
How can I add a new user to the list without reloading the page?
Is it stupid to constantly do setTimeout on a request to the server every 1-5 seconds?
Or is it somehow possible to make requests to the server only by the condition of adding a new user, and after adding to the list, stop requests to the server?
+ There is a possibility that the server may delay data for several minutes
Web sockets probably do not make sense to use
If you make a request to the server on the condition that if the new list of users === the old list, then make another request. And if someone else was added at that time, the list will be updated with the wrong data.
It's stupid to make a setTimeout, but what if the backend radish breaks?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Neverov, 2019-10-31
@TTATPuOT

Why not? Given that you want a refresh rate of 1-5 seconds, it makes sense.
Well, or make requests every 1-5 seconds.

G
Gor Mkhitaryan, 2019-10-31
@MrFeaf

Why doesn't it make sense to use WebSocket? This solution is better and smarter than knocking on the back. In addition, you can quickly implement such realtime solutions in a project if you already have a configured WebSocket server.

V
vadimMalovaniy, 2019-10-31
@vadimMalovaniy

send a request to the server > a response comes from the server > if the response is successful, update the data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question