Answer the question
In order to leave comments, you need to log in
How to implement likes?
The active like indicator depends on the class that is added if the like id matches the user id. When the page is loaded, everything is displayed normally, but when the like is clicked, I don’t understand how I can change the class.
function Like() {
const likeActive = (el) => el.likes.some(like => like._id === "ldjdljdljl030430430dmcdl");
return (<button className={` ${likeActive(el) && 'likeActive'}`}></button>)
}
Answer the question
In order to leave comments, you need to log in
Clicking on the like will send a request to the server. A response will come, according to which the data about likes (most likely, the el.likes array) will be updated. If they are correctly located in the state manager, then the component will redraw itself.
btw, https://www.npmjs.com/package/classnames
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question