May 2018
Intermediate to advanced
470 pages
13h 54m
English
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 ...
Read now
Unlock full access