The states will be cleaned up automatically for an unmounted component. The app code does not do anything for it. You may see the same discussed here.
The doc here says not to do the below code. The state setter inside a clean up to be removed. Though this documentation is outdated, the same point is there in the new documentation as well.
return () => {
// Cleanup function to clear data
setData([]);
};
Another discussion of the same kind can see here as well