I have this fairly simple code.
When I press the button I want to push a new exercise
object to the exercises
state array.
But only the second time I press the button an object is added...
import React, { useState } from "react";
import { Button } from "react-bootstrap";
const EditorMenuPage = () => {
const [exercises, setExercises] = useState([]);
const addNewExercise = () => {
const exercise = {
exercise: "",
duration: {
active: "None",
sets: null,
reps: null,
...,
},
...,
};
setExercises(() => [...exercises, exercise]);
console.log(exercises);
};
return (
<Button onClick={addNewExercise}>
Add exercise
</Button>
};
export default EditorMenuPage;
The console looks like this after pressing the button a couple of times:
The first log is empty I want this to already contain one exercise
object