1. Define state array variable
const [artists, setArtists] = useState([]);
2. Define with initial value
let initialArtists = [
{ id: 0, name: 'Marta Colvin Andrade' },
{ id: 1, name: 'Lamidi Olonade Fakeye'},
{ id: 2, name: 'Louise Nevelson'},
];
const [artists, setArtists] = useState(
initialArtists
);
3.Add a new row to Array.
setArtists([
...artists,
{
id: item.id,
name: item.name,
price: item.price,
image: item.image,
qantity: 1,
type: "normal",
},
])
4.Update a row in Array.
setArtists([...artists.filter((x) => x.id !== item.id),
{
id: item.id,
name: item.name,
type: item.type,
price: item.price,
image: item.image,
product_id: 0,
qantity:
[...artists].find((a) => a.id === item.id).qantity -
1, //If increment + 1 & decrement - 1
},
])
5. Delete a row in Array.
setArtists([...artists.filter((x) => x.id !== item.id)])
6. Fatch a row in Array.
setArtists([...artists.filter((x) => x.id !== item.id)])