I am using random user api to fetch user information with setInterval function and my useEffect looks like this;
// Users.js
useEffect(() => {
const getUser = () => {
fetch("https://randomuser.me/api")
.then((res) => res.json())
.then((data) =>
setUsers((prevUsers) => {
return setUsers([
...prevUsers,
{ key: data.results[0].login.uuid, value: data.results[0] },
]);
})
);
console.log("cleanup?");
};
getUser();
// const userInterval = setInterval(getUser, 5000);
// return () => clearInterval(userInterval);
}, []);
I use react navigation to display each user's details in another page and navigate like this;
<TouchableOpacity
onPress={() => navigation.navigate("userDetails", item.value)}>
So when I nagivate to details page, useEffect doesn't return which means component doesn't unmount. It actually makes because of the stack navigation, pages are basically on top of each and still running. So how can I stop my interval function in this case?