-1

Using const [projects, setProjects] = useState([]) I can update projects, but how can I push to projects at the first position?

I am using

setProjects([...projects, newData])

but that adds the newData at the end. I want it to be at the first position.

T. Karter
  • 638
  • 7
  • 25
  • Does this answer your question? [How can I add new array elements at the beginning of an array in Javascript?](https://stackoverflow.com/questions/8073673/how-can-i-add-new-array-elements-at-the-beginning-of-an-array-in-javascript) – jonrsharpe Aug 19 '20 at 10:43
  • Specifically https://stackoverflow.com/a/39531492/3001761, I'm surprised to didn't guess it from what you have. – jonrsharpe Aug 19 '20 at 10:43
  • 1
    @jonrsharpe Now, where I got the answer, me neighter – T. Karter Aug 19 '20 at 11:24

2 Answers2

2
setProjects([newData, ...projects ])
bakar_dev
  • 936
  • 4
  • 13
2

You're almost there -

setProjects([newData, ...projects])
Rycochet
  • 2,860
  • 1
  • 22
  • 39