I'm trying to figure out the working of React Hook API. I'm trying to add a number to a list. The code that I commented, i.e myArray.push... doesn't seem to perform the operation, though the code below it is working fine. Why is it so?
import React, {useState} from 'react'
export default () => {
const [myArray, setArray] = useState([1,2,3])
return (
<div>
{myArray.map((item=>{
return <li>{item}</li>
}))}
<button onClick = {()=>{
// myArray.push(myArray[myArray.length-1]+1)
// setArray(myArray)
setArray([...myArray, myArray[myArray.length-1]+1])
}}>Add</button>
</div>
)
}