I'm attempting to update the usersData variable created using useState so that anytime someone clicks on add user, a data object is added to the beginning of usersData, but the problem is that it only works with spread operators and not with push, pop, or unshift operations, as seen in the code below.
The code below is functioning great, and it updates and re-renders every time usersData changes:
const [usersData, setUsersData] = useState([]);
const receiveUserData = (data) => {
const dataUpdated = [data, ...usersData];
setUsersData(dataUpdated);
};
However, dataUpdated has the same data as above code, the following code does not re-renders the page:
const [usersData, setUsersData] = useState([]);
const receiveUserData = (data) => {
let dataUpdated = usersData;
dataUpdated.unshift(data);
setUsersData(dataUpdated);
};