I would like the updated state of the array to be written to the console on the first click. I know that this is done through a callback, but I just can’t understand and find someone to do this in functional components. I would like the updated state of the array to be written to the console on the first click. I know that this is done through a callback, but I just can’t understand and find someone to do this in functional components.
import "./App.css";
import { useState } from "react";
function App() {
const [name, setName] = useState("");
const [array, setArray] = useState([]);
function createNameList(e) {
setArray((array) => [...array, e]);
console.log(array);
}
return (
<div className="App">
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<button onClick={createNameList}>accept</button>
</div>
);
}
export default App;