I have this React Component for Privates routes. I want to check if user is logged in, so I check if jwt is in local storage and if token is not expired. If this doesn't happen I want to log user out, since token is expired.
I have a react warning that says I can't return a function and Component at the same level. What would be the best approach to achieve this?
import React from 'react';
import { Navigate, Outlet } from 'react-router-dom';
import dayjs from 'dayjs';
import { useUserStore } from '@store/userStore';
import Main from '@layouts/Main';
function PrivateRoutes() {
const { expiresIn, removeUser } = useUserStore();
const now = dayjs();
const handleRemoveUser = () => {
removeUser();
};
return window.localStorage.getItem('jwt') && dayjs(expiresIn).isAfter(now) ? (
<Main>
<Outlet />
</Main>
) : (
<>
{handleRemoveUser}
<Navigate to='/login' />
</>
);
}
export default PrivateRoutes;
I have tried using handler functions, arrow functions but not sure how to approach this situation