Skip to content

Instantly share code, notes, and snippets.

@amariliscamargo
Forked from tjercus/like-dislike.js
Created July 3, 2022 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amariliscamargo/bd80947080d3bd2cfca0039be3759576 to your computer and use it in GitHub Desktop.
Save amariliscamargo/bd80947080d3bd2cfca0039be3759576 to your computer and use it in GitHub Desktop.
like-dislike buttons. Created with create-react-app
import React, { Component } from "react";
import "./App.css";
const reduceOne = (prevState, groupName, otherGroupName) => {
prevState[groupName].wasClicked
? prevState[groupName].count = prevState[groupName].count - 1
: prevState[groupName].count = prevState[groupName].count + 1;
prevState[groupName].wasClicked = !prevState[groupName].wasClicked;
if (prevState[otherGroupName].wasClicked) {
prevState[otherGroupName].count = prevState[otherGroupName].count - 1;
prevState[otherGroupName].wasClicked = false;
}
return prevState;
};
const reducer = action =>
(prevState, props) =>
action.type === "TOGGLE_LIKE"
? reduceOne(prevState, "like", "dislike")
: reduceOne(prevState, "dislike", "like");
class App extends Component {
state = {
like: {
count: 0,
wasClicked: false,
},
dislike: {
count: 0,
wasClicked: false,
},
};
toggleLike = () => this.setState(reducer({ type: "TOGGLE_LIKE" }));
toggleDislike = () => this.setState(reducer({ type: "TOGGLE_DISLIKE" }));
render() {
return (
<div className="App">
<header className="App-header">
<h1 className="App-title">Like - Dislike</h1>
</header>
<p className="App-intro">
<button className="like-button" onClick={this.toggleLike}>
Like | {this.state.like.count}
</button>
<button className="dislike-button" onClick={this.toggleDislike}>
Dislike | {this.state.dislike.count}
</button>
</p>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment