I wrote a simple component in React which is supposed to remove the last element of an array when a button is clicked, but despite console logging the correct value, my React state is not being updated accordingly (when viewing the components with React Developer Tools) and hence the output is not changing. I'm at a loss as to why it won't update correctly.
const Test = () => {
const [input, setInput] = useState(["c", "a", "t"])
const removeLastElementFromArray = () => {
setInput(prev => {
console.log("Previous array was:", prev)
let t = prev.pop();
console.log("Array popped, array is now:", prev)
return prev;
})
}
return (
<>
<button onClick={removeLastElementFromArray}>Click</button>
{input.map((char, index) => <span key={index}>{char}</span>)}
</>
)
}