April 2020
Intermediate to advanced
716 pages
18h 55m
English
To handle clicks on the like and unlike buttons, we will set up a clickLike method that will call the appropriate fetch method based on whether it is a "like" or "unlike" action, and then update the state of the like and likes count for the post. This clickLike method will be defined as follows.
mern-social/client/post/Post.js:
const clickLike = () => { let callApi = values.like ? unlike : like const jwt = auth.isAuthenticated() callApi({ userId: jwt.user._id }, { t: jwt.token }, props.post._id).then((data) => { if (data.error) { console.log(data.error) } else { setValues({...values, like: !values.like, likes: data.likes.length}) } }) }
Which of the like or unlike API endpoints will be called on a click depends on the ...
Read now
Unlock full access