Programmatic Navigation with useRouter in Next.js
📖 Scenario: You are building a simple Next.js app with two pages: a homepage and a profile page. You want to add a button on the homepage that takes the user to the profile page when clicked.
🎯 Goal: Build a Next.js component that uses the useRouter hook to navigate programmatically from the homepage to the profile page when a button is clicked.
📋 What You'll Learn
Create a functional component called
HomePageImport and use the
useRouter hook from next/routerCreate a button with the text
Go to ProfileAdd a click handler that uses
router.push('/profile') to navigate💡 Why This Matters
🌍 Real World
Programmatic navigation is common in web apps to move users between pages based on actions like button clicks or form submissions.
💼 Career
Understanding useRouter and programmatic navigation is essential for building interactive Next.js applications and is a common task in frontend development roles.
Progress0 / 4 steps