Skip to content

Instantly share code, notes, and snippets.

@pham-andrew
Created February 10, 2022 18:04
Show Gist options
  • Save pham-andrew/1aaf8570fe092bc47026074c3f64925f to your computer and use it in GitHub Desktop.
Save pham-andrew/1aaf8570fe092bc47026074c3f64925f to your computer and use it in GitHub Desktop.
React Reddit Voting
const [vote, setVote] = useState<Vote>("none");
const handleVote = (v: Vote) => {
let increment = 0;
if (v === "up") {
increment = 1;
setVote("up");
}
if (v === "down") {
increment = -1;
setVote("down");
}
if (vote === "down" && v === "up") increment = 2;
if (vote === "up" && v === "down") increment = -2;
if (vote === "down" && v === "down") {
increment = 1;
setVote("none");
}
if (vote === "up" && v === "up") {
increment = -1;
setVote("none");
}
setPoints(points + increment);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment