The array you pass as second argument to useEffect
only checks if the elements in the array are ===
to the elements in it in the previous render. const newArr = arr;
will lead to newArr === arr
since it doesn’t create a new array, which is not what you want.
Create a new array with all the elements in arr
and it will work as expected.
const App = props => {
const { arr, setArr } = useContext(GlobalContext)
const handleChange = () => {
const newArr = [...arr]
[10, 20, 30, 40].forEach(v => {
newArr.push(v)
})
setArr(newArr)
}
return <>{/* ... */}</>
}