I'm trying to update an array using react's useState, however it is always 1 step behind.
I made a minimal reproduction using here:
Surely I'm misunderstanding how useState works as the onChange value triggers correctly.
export default Home(){
const [inputData, setInputData] = useState<{ exercise: string; weight: number }[]>([]);
function procesWeightInput(exercise: string, weight: number) {
const input = { exercise, weight };
setInputData([input]);
console.log(inputData);
}
return (
<input
type="number"
placeholder="Weight"
onChange={(e) => {
console.log("The value", e.target.value);
procesWeightInput(exercise.name, +e.target.value);
}}
/>
)