I have a multipage application. When i click between dashboard/global and dashboard/my-posts useEffect is invoked. Thus, my application is constantly calling my fetch and taking a while to load.
Is there a way to only invoke useEffect when new data is being has been found?
I tried adding myRecipes and AllRecipes to the useEffect dependency but track promise's loading indictator is running infinitely.
import React, {
useState, useEffect, useContext, useMemo,
} from 'react';
import { v1 as uuidv1 } from 'uuid';
import { trackPromise } from 'react-promise-tracker';
import LoadingIndicator from '../utils/LoadingIndicator';
import DashboardHeader from './DashboardHeader';
import '../App.css';
import Post from './Post';
import CreateForm from '../create-recipe/CreateForm';
import RecipeService from '../../service/RecipeService';
import { AuthContext } from '../../context/AuthContext';
export default function Dashboard() {
const { isAuthenticated } = useContext(AuthContext);
const [allRecipes, setAllRecipes] = useState([]);
const [myRecipes, setMyRecipes] = useState([]);
const currentUrl = window.location.pathname;
useEffect(() => {
if (currentUrl === '/dashboard/global') {
trackPromise(
RecipeService.getAllRecipes()
.then((data) => {
setAllRecipes(data);
}),
);
} else if (currentUrl === '/dashboard/my-posts' && isAuthenticated === true) {
trackPromise(
RecipeService.getRecipes()
.then((data) => {
setMyRecipes(data);
}),
);
}
}, [currentUrl, allRecipes, myRecipes]);
return (
<>
<div className="dashboard">
<DashboardHeader />
{currentUrl === '/dashboard/my-posts' && !isAuthenticated
? <h2 className="dashboard__unauthenticated-msg">Please Login To See Your Recipes</h2>
: null}
<div className="created-posts">
{currentUrl === '/dashboard/global' && allRecipes
? allRecipes.map((recipe) => <Post recipe={recipe} key={uuidv1()} />)
: null}
{currentUrl === '/dashboard/my-posts' && myRecipes.recipes
? myRecipes.recipes.map((recipe) => <Post recipe={recipe} key={uuidv1()} />)
: null}
{myRecipes.recipes && allRecipes
? null : <LoadingIndicator />}
</div>
{currentUrl === '/dashboard/create' ? <CreateForm /> : null}
</div>
</>
);
}