const page = () => {
const router = useRouter();
const {data, isLoading} = useGetUser();
useEffect() => ({
if (isLoading) return;
if (!data) {
router.push('/create-account');
}
}, [isLoading, data])
if (isLoading) {
return <spinner />
}
return (
<Home />
)
}
I need to redirect to the create-account
page if a user is not authenticated.
The above code works well. But it redirects to create-account
page when I navigate the home page after signing in.
I debugged and I found the isLoading
and data
will be false
together when the page is loading the first time.
Is there any way to prevent this?
3
Answers
This will resolve your issue
The answer from @Muammad Ahab should work, but you can use a React hook as well:
Have you tried using
useLayoutEffect
hook? This hooks runs before the page is painted.