AIM: To fetch an api on the web application load on the project level. Because, the application in dependent on this api call. So, If there a place where I can call an api on project level. Please suggest that solution as well.
Sample code:
import "./styles.css";
import { Routes, Route } from "react-router-dom";
import { useEffect } from "react";
export default function App() {
useEffect(() => {
APICall();
},[])
if (isLoading) { // Getting the loader state from
//API call
return <Spinner />;
}
return <Routes>
<Route element={<HomePage />} path="/">
</Routes>;
}
Doubt: Is this best practice to call an API on the mainroutes and make the spinner load till the API is fetched or else will this cause any side effect. If possible please suggest an alternate solution for this issue.
There're many solutions. But mostly it depends on your project complexity. For really simple projects it's not ideal, but it's okay to make as you did. But I would highly recommend letting the fetch responsibility to your components that really need those data - so you should think about breaking your components
ProtectedRoutes which returns 2 components conditionally - UnProtectedPage and Component.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With