Complete the code to import the Next.js router hook.
import { [1] } from 'next/navigation';
The correct hook to access the router in Next.js client components is useRouter.
Complete the code to get the current pathname from the router.
const router = useRouter();
const currentPath = router.[1];The pathname property gives the current path in Next.js router.
Fix the error in the code to prefetch a route for caching.
const router = useRouter(); router.[1]('/about');
The prefetch method tells Next.js to load the page in the background for faster navigation.
Fill both blanks to create a client component that prefetches the home route on mount.
import { useEffect } from 'react'; import { useRouter } from 'next/navigation'; export default function PrefetchHome() { const router = useRouter(); useEffect(() => { router.[1]('[2]'); }, []); return null; }
Use prefetch to load the '/' route on mount for caching.
Fill all three blanks to create a component that prefetches two routes and logs the current path.
import { useEffect } from 'react'; import { useRouter } from 'next/navigation'; export default function MultiPrefetch() { const router = useRouter(); useEffect(() => { router.[1]('[2]'); router.[3]('/contact'); console.log('Current path:', router.pathname); }, []); return null; }
Use prefetch to cache both '/about' and '/contact' routes on mount.
