Remove first element of an array in React useState hooks?

Viewed 1483

I have a state like so:

const [array, setArray] = useState([])

I know I can use below code to add an item to an array:

setArray(oldArray => [...oldArray, item])

How can I remove the first element of this array?

3 Answers
const names = ['Luke', 'Eva', 'Phil'];
var [first, ...rest] = names;  
console.log(rest); // ['Eva','Phil']  

source

You can also use Array.prototype.slice():

const arr = ['item 1', 'item 2', 'item 3', 'item 4']

console.log(arr.slice(1))

setArray(oldArray => oldArray.filter(_,i) => i !== 0))
Related