Handling like clicks

To handle clicks on the like and unlike buttons, we will set up a like method that will call the appropriate fetch method based on whether it is a like or unlike action, and update the state of like and likes count for the post.

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

like = () => {    let callApi = this.state.like ? unlike : like     const jwt = auth.isAuthenticated()    callApi({      userId: jwt.user._id    }, {      t: jwt.token    }, this.props.post._id).then((data) => {      if (data.error) {        console.log(data.error)      } else {        this.setState({like: !this.state.like, likes:        data.likes.length})      }    })   }

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.