Fixing useEffect warnings

This commit is contained in:
aeroreyna
2022-11-19 20:45:32 -05:00
parent 4e1112fbf4
commit a313822c27
6 changed files with 81 additions and 56 deletions

View File

@@ -31,30 +31,34 @@ const getFeed = async () => {
let Feed = ({ navigation, route }) => {
let [Posts, setPosts] = useState([]);
console.log("Render Feed");
useEffect(async () => {
let loggedIn = await API.isLoggedIn();
if (!loggedIn) return navigation.reset({
index: 0,
routes: [{ name: 'Login' }],
});
if (route.params && route.params.profileid) {
return navigation.navigate('Profile', { profileid: route.params.profileid })
}
useEffect(() => {
let subscribed = true;
API.getMe().then((me) => {
if (subscribed)
GlobalState.me = me;
});
console.log("Feed from cache")
let cacheFeed = await getFeed() || [];
if (cacheFeed.length && subscribed) setPosts(cacheFeed);
console.log("Feed from server")
let posts = await API.getPosts();
if (subscribed) {
setPosts(posts);
storeFeed(posts);
const getData = async () => {
let loggedIn = await API.isLoggedIn();
if (!loggedIn) return navigation.reset({
index: 0,
routes: [{ name: 'Login' }],
});
if (route.params && route.params.profileid) {
return navigation.navigate('Profile', { profileid: route.params.profileid })
}
API.getMe().then((me) => {
if (subscribed)
GlobalState.me = me;
});
console.log("Feed from cache")
let cacheFeed = await getFeed() || [];
if (cacheFeed.length && subscribed) setPosts(cacheFeed);
console.log("Feed from server")
let posts = await API.getPosts();
if (subscribed) {
setPosts(posts);
storeFeed(posts);
}
console.log("Feed, end useEffect")
}
console.log("Feed, end useEffect")
getData()
return () => {
subscribed = false;
}

View File

@@ -33,28 +33,31 @@ let Profile = ({ navigation, route }) => {
let [Posts, setPosts] = useState([]);
let [profile, setProfile] = useState({});
useEffect(async () => {
useEffect(() => {
let subscribed = true;
setPosts([]);
if (route.params && route.params.profileid) {
console.log('Loading Cache Profile:' + route.params.profileid);
await API.getUserProfile(route.params.profileid).then((profileObj) => {
if(!subscribed) return 0;
let profile = profileObj.profile
setProfile(profileObj);
navigation.setOptions({ title: profile.firstName + " " + profile.lastName });
});
await getProfilePosts(route.params.profileid).then(setPosts);
console.log('Loaded Cache Profile:' + route.params.profileid);
API.getPosts(route.params.profileid).then((data) => {
if(!subscribed) return 0;
setPosts(data);
storeProfilePosts(route.params.profileid, data);
console.log('Store Cache Profile:' + route.params.profileid);
});
} else {
navigation.navigate('Feed')
const getData = async () => {
setPosts([]);
if (route.params && route.params.profileid) {
console.log('Loading Cache Profile:' + route.params.profileid);
await API.getUserProfile(route.params.profileid).then((profileObj) => {
if(!subscribed) return 0;
let profile = profileObj.profile
setProfile(profileObj);
navigation.setOptions({ title: profile.firstName + " " + profile.lastName });
});
await getProfilePosts(route.params.profileid).then(setPosts);
console.log('Loaded Cache Profile:' + route.params.profileid);
API.getPosts(route.params.profileid).then((data) => {
if(!subscribed) return 0;
setPosts(data);
storeProfilePosts(route.params.profileid, data);
console.log('Store Cache Profile:' + route.params.profileid);
});
} else {
navigation.navigate('Feed')
}
}
getData();
return ()=>{
subscribed = false;
}