So I’m with an issue in my react app that I can’t seem to solve, already for 2 days. I have my routes defined in my App.js file, with loader methods. One of these routes (/myreputation) has a loader method that executes a fetch request to my backend server. The problem is, whenever I refresh the page (after the request is made), my application gets broken, as if the css gets all messed up. This is the App.js file where I define the loader method (getReputationLoader):
const router = createBrowserRouter([
{
path: "/",
element: <RootLayout />,
errorElement: <ErrorPage />,
children: [
{ path: "/", element: <HomePage /> },
{ path: "/home", element: <HomePage /> },
{ path: "/help", element: <HelpPage /> },
{ path: "/signup", element: <SignupPage /> },
{ path: "/signin", element: <SigninPage /> },
{ path: "/resetpwform", element: <ResetPasswordFormPage /> },
{ path: "/emailSent", element: <EmailSentPage /> },
{ path: "/setnewpassword", element: <SetPasswordPage /> },
{ path: "/resetsuccessful", element: <SuccessfulResetPage /> },
{ path: "/editprofile", element: <EditPage />, loader: checkAuthLoader },
{
path: "/myreputation",
element: <ReputationPage />,
loader: getReputationLoader,
},
{
path: "/makeclassification",
element: <MakeClassificationPage />,
loader: checkAuthLoader,
},
{
path: "/codeanalysis",
element: <CodeSnippetAnalysisPage />,
loader: checkAuthLoader,
},
{
path: " /myclassifications",
element: <MyClassificationsPage />,
loader: checkAuthLoader,
},
{ path: "/overview", element: <OverviewPage /> },
{ path: "/error", element: <ErrorPage /> },
{ path: "/unauthorized", element: <UnauthorizedPage /> },
{ path: "/tests", element: <Tests /> },
],
},
]);
function App() {
const [isDarkTheme, setDarkTheme] = useState(false);
const [isLoggedIn, setIsLoggedIn] = useState(false);
const token = getAuthToken();
const tokenDuration = getTokenDuration();
useEffect(() => {
if (token) {
setIsLoggedIn(true);
setTimeout(() => {
localStorage.removeItem("token");
localStorage.removeItem("tokenExpirationDate");
setIsLoggedIn(false);
}, tokenDuration); //7 days token duration before expires - 604800000 in ms
}
}, [token, tokenDuration]);
const themeHandler = () => {
setDarkTheme((prev) => !prev);
};
const handleLogin = () => {
setIsLoggedIn(true);
};
const handleLogout = () => {
setIsLoggedIn(false);
};
const theme = isDarkTheme ? darkTheme : lightTheme;
return (
<StyledEngineProvider injectFirst>
<DarkThemeContext.Provider
value={{
handler: themeHandler,
}}
>
<AuthenticationStatusContext.Provider
value={{
isLoggedIn: isLoggedIn,
loginHandler: handleLogin,
logoutHandler: handleLogout,
}}
>
<ThemeProvider theme={theme}>
<GlobalStyles></GlobalStyles>
<RouterProvider router={router} />
</ThemeProvider>
</AuthenticationStatusContext.Provider>
</DarkThemeContext.Provider>
</StyledEngineProvider>
);
}
And this is the MyReputation page where I use the data fecthed alongside with the loader method:
const ReputationPage = () => {
const reputationData = useLoaderData();
//console.log(reputationData);
return (
<Fragment>
<ReputationTable data={reputationData}></ReputationTable>
</Fragment>
);
};
export async function getReputationLoader() {
//checkAuthLoader();
const token = localStorage.getItem("token");
const decodedToken = jwt_decode(token);
const userId = decodedToken.unique_name;
try {
const response = await fetch(
`https://localhost:7182/api/v1/users/reputation/${userId}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
}
);
console.log("AQUI 1");
if (!response.ok) {
//throw new Error();
return;
} else if (response.ok) {
const responseInfo = await response.json();
return responseInfo;
}
} catch (error) {
redirect("/error");
}
}
I feel like the app is not rendering after that request. Whenever I don’t use that loader (getReputationLoader) everything works just fine. I really can’t find a solution to this. Thank you for your time.