skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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 a Navigate component to issue a declarative redirect. The component will render at least once.

    Example:

    if (!cookie) {
      return <Navigate to="/login" replace />;
    }
    
    ...
    
    return /* normal JSX return */
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search