When I click on the add button the state updates but then the list still shows the same stuff. How do I fix it so that it updates with state?
I'm also using sortablejs but I removed it from the code since it doesn't seem to be relevant and I didn't want to clutter the code with unnecessary packages.
import React, { useState } from "react";
import { ListGroup } from 'react-bootstrap';
const DraggableList = () => {
const [events, setEvents] = useState([
{ id: 1, name: "shrek" },
{ id: 2, name: "fiona" },
{ id: 3, name: "donkey" },
]);
const addEvent = () => {
let newEvents = events
let event = {
id: events.length + 1,
name: "dragon",
}
newEvents.push(event)
setEvents(newEvents)
console.log(events)
}
return (
<div>
<button onClick={addEvent}>Add</button>
{events.map((item) => (
<ListGroup.Item key={item.id} action variant="success">{item.name}</ListGroup.Item>
))}
</div>
);
};
export default DraggableList