I am building a web application using React. I have used React Lazy with Webpack for code splitting but there is one issue that I have encountered. If I am on page A and I visit another screen (B) and now I re-visit the last screen (A) the screen is loaded again and the API calls in the useEffect hook of the component (A) is executed again.
My app does not have data that will constantly change so I dont want to call it again and again. What I concluded was that when the users visits another screen React unmounts the previous component and when the user visits again it mounts the component and executes the useEffect hook.
So, there are unnecessary API calls that I want to avoid. I know that I can cache my API calls but that feels like a workaround. Is there a better alternative to API caching or have I made some mistake in my code?
My app.js file where code splitting is implemented:
const LoginContainer = lazy(()=> import('./Container/StudentDashboard.container'));
My StudentDashboard.container.js file:
const StudentDashboard = ()=>{
useEffect(()=>{
(async()=>{
const data = await fetch({...}) # this API call is executed every time user visits the screen
})
},[])
}