As far as I understand react keeps track of prevState, when state is updated. Then how is the memory garbage collected for prevState? Is it garbage collected automatically when a new state is set. Or it still holds the memory for prevState?
export default function App() {
const arr = Array.from({ length: 5 }, () => Math.floor(Math.random() * 5));
const [myState, setMyState] = useState(arr);
const handleClick = () => {
setMyState([]);
// Will this automatically de-allocate memory for the last state array?
// OR
// Will the prevState array will still be in memory heap.
// If yes, how to de-allocate this memory!
};
return (
<div className="App">
<button onClick={() => handleClick()}>Clear Array</button>
</div>
);
}