I was just playing around with few things and observed this behavior; I can't find the reason anywhere. I have an initial array state [1, 2], in useEffect I have been updating the state based on prev values
const [myArr, setArray] = useState([1, 2])
useEffect(() => {
setArray((prevValues) => [...prevValues, ...newData]);
}, [])
Every time I reload my app, the state is keep on appending newData to prevValues, How come the state is not updating based on initial array values? How come state knows the previous array even though I refresh the emulator?
Instead of this,
newData = [3,4]
myArr = [1, 2, 3, 4]
updating like this,
newData = [3,4]
myArr = [1, 2, 3, 4, 3, 4, 3, 4, 3, 4]
Is it wrong to update state values based on prev values in useEffect? Want to understand this behavior.