I want to redirect page without render.
Index page
useEffect(() => {
if (!cookie) {
router.push('/login')
}
}, []);
When I open index page it retain for few second then redirect to login page. Can avoid to render it?
I want to redirect page without render.
Index page
useEffect(() => {
if (!cookie) {
router.push('/login')
}
}, []);
When I open index page it retain for few second then redirect to login page. Can avoid to render it?
2
Answers
useEffect runs after the html elements are rendered/added to the DOM.
That’s why the index component is being rendered first, and then you are being redirected to login.
The
useEffect
hook runs at the end of the render cycle. If you would like to redirect when the component is rendered then conditionally early return render aNavigate
component to issue a declarative redirect. The component will render at least once.Example: