From 7a85d56e787f5e90c4c5e08e1ef0d4a7c50d64a7 Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Wed, 26 Apr 2023 22:24:39 -0400 Subject: ex 1.13 add vote counter for each anecdotes utilzing arrays in hooks --- part1/anecdotes/src/App.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/part1/anecdotes/src/App.js b/part1/anecdotes/src/App.js index 4e33e98..8bc3b2b 100644 --- a/part1/anecdotes/src/App.js +++ b/part1/anecdotes/src/App.js @@ -8,12 +8,6 @@ const Button = (props) => { ) } -const Display = (props) => { - return ( -

{props.text} {props.count}

- ) -} - const App = () => { const anecdotes = [ 'If it hurts, do it more often.', @@ -28,6 +22,8 @@ const App = () => { ] const [selected, setSelected] = useState(0) + const [votes, setVotes] = useState(Array(anecdotes.length).fill(0)) + const getRandomInt = (min, max) => { min = Math.ceil(min) @@ -35,9 +31,17 @@ const App = () => { return Math.floor(Math.random() * (max - min) + min) } + const updateVotes = (selected) => { + const copy = [...votes] + copy[selected] += 1 + return copy + } + return (

{anecdotes[selected]}

+

has {votes[selected]} votes

+
) -- cgit v1.2.3