Checking if liked and counting likes

When the Post component is rendered, we need to check if the currently signed in user has liked the post or not, so the appropriate like option can be shown.

mern-social/client/post/Post.js:

checkLike = (likes) => {    const jwt = auth.isAuthenticated()    let match = likes.indexOf(jwt.user._id) !== -1    return match}

The checkLike function can be called during componentDidMount and componentWillReceiveProps of the Post component, to set the like state for the post after checking if the current user is referenced in the post's likes array:

The like value set in the state using the checkLike method can be used to ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.