why doesn't the vote update immediately?
import React, { useState } from "react";
import ReactDOM from "react-dom";
const Button = ({ onClick, text }) => {
return <button onClick={onClick}>{text}</button>;
};
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
const App = (props) => {
const [selected, setSelected] = useState(0);
const handleClick = () => {
setSelected(getRandomInt(min, max));
};
const vote = () => {
copy[selected] += 1;
sortedArr = [...copy].sort((a, b) => a - b);
return copy[selected], copy, sortedArr;
};
return (
<div>
<h2>anecdote of the day</h2>
{props.anecdotes[selected]} has {copy[selected]} votes
<br></br>
<Button onClick={handleClick} text={"next anecdote"} />
<Button onClick={vote} text="vote" />
<h2>anecdote with most votes</h2>
<p>
{anecdotes[copy.indexOf(sortedArr[sortedArr.length - 1])]} has{" "}
{sortedArr[sortedArr.length - 1]} votes
</p>
</div>
);
};
const anecdotes = [
"If it hurts, do it more often",
"Adding manpower to a late software project makes it later!",
"The first 90 percent of the code accounts for the first 90 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.",
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand.",
"Premature optimization is the root of all evil.",
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.",
];
const max = anecdotes.length;
const min = 0;
const points = Array(max).fill(0);
const copy = [...points];
let sortedArr = [...copy];
ReactDOM.render(<App anecdotes={anecdotes} />, document.getElementById("root"));