E
E
Elena2021-12-16 15:59:19
React
Elena, 2021-12-16 15:59:19

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

1 answer(s)
A
Alexandroppolus, 2021-12-16
@Elena0394

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 question

Ask a Question

731 491 924 answers to any question