Home > database >  unshift posted to object in react js
unshift posted to object in react js

Time:11-22

I have a stata which fills by fetched data from API. here is my code: enter image description here

Now I wanna add new data to first of my table. but I don't know how to do it: enter image description here

Here new data will push to end of table :(

Can everyone help me?

CodePudding user response:

This will push the data to be the last in the array:

const tempArray=allClient;
tempArray.push(data)
setAllClient([...tempArray])

This will put the data to be the first in the array:

const tempArray=allClient;
tempArray.unshift(data)
setAllClient([...tempArray])

CodePudding user response:

It was helpful for me

setAllClient(currentArray => [data, ...currentArray]);
  • Related