I am trying to add an item to an array in ReactJS code, but the new item (greetings) does not add to the array, what would be the potential error in the blow code?
import { useState } from "react";
export default function App() {
// an array of items
const [tags, setTags] = useState(["happy", "cheers", "wonderful"]);
const handleClick = () => {
console.log("before adding an item");
console.log(tags);
setTags([...tags, "greetings"]);
console.log("after adding an item");
console.log(tags);
};
return (
<div>
<button onClick={handleClick}>
Add an item to the array
</button>
</div>
);
}
//export default App;
New contributor
panther is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.