like button react

Solutions on MaxInterview for like button react by the best coders in the world

showing results for - "like button react"
Elijah
02 Apr 2018
1import React from 'react';
2
3class Likes extends React.Component {
4
5  constructor(props){
6
7    super(props);
8    this.state = {
9      likes: 124,
10      updated: false
11    };
12
13  }
14
15  updateLikes = () => {
16
17    if(!this.state.updated) {
18      this.setState((prevState, props) => {
19        return {
20          likes: prevState.likes + 1,
21          updated: true
22        };
23      });
24
25    } else {
26
27      this.setState((prevState, props) => {
28        return {
29          likes: prevState.likes - 1,
30          updated: false
31        };
32      });
33
34    }
35  }
36
37  render(){
38
39    return(
40      <div>
41        <p onClick={this.updateLikes}>Like</p>
42        <p>{this.state.likes}</p>
43      </div>
44    );
45  }
46}
47
48export default Likes;
49
50
51
52
53