Skip to content

Instantly share code, notes, and snippets.

@miguelzinhe
Created April 30, 2020 22:59
Show Gist options
  • Save miguelzinhe/2315d58134f67b852fa235c9219f1cb0 to your computer and use it in GitHub Desktop.
Save miguelzinhe/2315d58134f67b852fa235c9219f1cb0 to your computer and use it in GitHub Desktop.
Challenge C*d*l*ty Like
import classNames from 'classnames'
import { Component } from 'react'
export default class LikeButton extends Component {
constructor(props) {
super(props);
this.state = { liked: false, count: 100 };
}
onClickAddLikeHandler = () => {
this.setState({ count: this.state.count + 1, liked: true });
};
onClickRemoveLikeHandler = () => {
this.setState({ count: this.state.count - 1, liked: false });
};
render() {
const { liked, count } = this.state;
return (
<>
<div>
<button
className={classNames("like-button", { liked })}
onClick={
liked ? this.onClickRemoveLikeHandler : this.onClickAddLikeHandler
}
>
Like | <span className="likes-counter">{count}</span>
</button>
</div>
<style>{`
.like-button {
font-size: 1rem;
padding: 5px 10px;
color: #585858;
}
.liked {
font-weight: bold;
color: #1565c0;
}
`}</style>
</>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment