how to update a state with an array react code example
Example 1: how to update react state array
const [myArray, setMyArray] = useState([1,2,3])
// to update
setMyArray([...myArray, 4]); // --> myArray = [1,2,3,4]
Example 2: how to update a state with an array react
const [array, setArray] = useState([1, 2, 3])
// Add
setArray(prevArray => [...prevArray, 4]); // -> [1, 2, 3, 4]
// Remove last entry
setArray(prevArray => prevArray.splice(0, prevArray.length - 1)); // -> [1, 2, 3]