I am building a blog site and there is a like functionality to like comments of users.
I get the comments and corresponding likers with an API call and assign them to state. If a user likes a comment, I call a POST request with the user_id and append the likers list in the database.
My question is; should I make a get request for the comments after like and assign them to the state and check whether the user_id is in the likers list,
OR should I append the state in the first place before/after put request to the database. Without the get request to assign the new likers list to the state.
I ask this question because it takes some time to get the new likers list and check whether the user_id is in the list. So after clicking the like button it takes 1-2 seconds to actually like the comment and make the heart red.
Thanks in advance.
A GET is not necessary after the POST. The purpose of having a response from the server is precisely so that you know that either a) your request/transaction went through successfully, or b) it failed and you need to handle why.
The best solution would be to update the state if and only if you get a 200 response code on the server from your POST request with the "like" information. Generally, it's best to update state only when you know that it's persisted in your database. Not doing so can lead to inconsistency if, say, the request failed and now you are showing a post is liked but as soon as they refresh, the "like" is gone (since it's not saved).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments