Created
February 10, 2022 18:04
-
-
Save pham-andrew/1aaf8570fe092bc47026074c3f64925f to your computer and use it in GitHub Desktop.
React Reddit Voting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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