Tambahkan array objek ke negara bereaksi
setTheArray(currentArray => [...currentArray, newElement])
Jeff Le
setTheArray(currentArray => [...currentArray, newElement])
const handleAdd = (todo) => {
const newTodos = [...todos];
newTodos.push(todo);
setTodos(newTodos);
}
this.setState( prevState => ({
userFavorites: [...prevState.userFavourites, {id: 3, title: 'C'}]
}));
To push to the beginning of the array do it this way
this.setState( prevState => ({
userFavorites: [{id: 3, title: 'C'}, ...prevState.userFavourites]
}));