React how should be the like functionality

Xelanor

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.

rb612

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.

edited at
0

Comments

0 comments
Login to comment

Related

How should I test functionality accessible only during the lifetime of an async method using @testing-library/react?

How to achieve group by like functionality in excel

How to create a like functionality in django for a blog

How to do '@somename' like twitter functionality?

How to Implement tab functionality in React

Better way to implement 'like tweet' functionality with react native and redux

React-native + mobx how the Index.js should look like

Not sure how to configure this tooltip functionality in react native

How to correctly implement the delete functionality in React Redux

How to organize functionality across react components?

How do I build like functionality using django and djangorestframework?

How to provide whatsapp like functionality inside contacts apps?

How to get Back button like functionality android studio

How to get "string-like" join functionality for arrays in Ruby

Angular: How to implement ng-include like functionality in Angular 5?

How to implement Promise.all like functionality in kotlin

How to implement functionality like Facebook "New Story" feature?

How to create a Python tkinter Frame with button-like functionality?

How to create a VForm component with validation functionality like Vuetify in Vue 2?

How do websites like jam.dev achieve their screenshotting functionality?

StorageItemAccessList like functionality in Android

PIVOT like functionality

Pencil-like functionality

Like functionality in Django

How should BMP header look like?

How should look like a morphline for MapReduceIndexerTool?

How should an inline SVG look like

How application structure should looks like?

How should "baseSiteUrl" look like in SPSite?