April 2020
Intermediate to advanced
716 pages
18h 55m
English
When the Post component is rendered, we need to check if the currently signed-in user has liked the post or not so that the appropriate like option can be shown. The following checkLike method checks whether the currently signed-in user is referenced in the post's likes array or not.
mern-social/client/post/Post.js:
const checkLike = (likes) => { let match = likes.indexOf(jwt.user._id) !== -1 return match}
This checkLike function can be called while setting the initial value of the like state variable, which keeps track of whether the current user liked the given post or not. The following screenshot shows how the like button renders when a post has not been liked versus when it has been ...
Read now
Unlock full access