skip to Main Content

useRouter from next/navigation does not load the route immediately and i have to wait a littel an it seems the click is not registered, it waits for the page to render on server side then redirect neither the url changes in the browser till the page is rendered on server side.what i want is the url should change and the data for page should load on the redirected page

2

Answers


  1. You can add a loader to avoid this problem.
    Just add a loading.tsx in your folder which has your layout.tsx file.
    This loader will prevent your said delay.

    You can check how to add loading here

    Login or Signup to reply.
  2. Try Adding a loading indicator during the route change to get a visual feedback that the page is loading.

    const [loading, setLoading] = useState(false);
    const fetchData = async () => {
    setLoading(true);
    try {
      const response = await fetch('endpoint');
     
      setLoading(false);
      router.push("/url")
    } catch (error) {
      console.error('Error fetching data:', error);
      setLoading(false);
    }
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search