Complete the code to import the hook needed for navigation in Next.js.
import { [1] } from 'next/navigation';
The useRouter hook from next/navigation is used for programmatic navigation in Next.js.
Complete the code to get the router object inside a functional component.
const router = [1]();Calling useRouter() inside a component returns the router object for navigation.
Fix the error in the code to navigate to '/about' page on button click.
function GoToAbout() {
const router = useRouter();
return (
<button onClick={() => router.[1]('/about')}>Go to About</button>
);
}The correct method to navigate programmatically is router.push().
Fill both blanks to create a button that navigates to '/contact' page using the router.
function ContactButton() {
const router = [1]();
return (
<button onClick={() => router.[2]('/contact')}>Contact Us</button>
);
}You need to call useRouter() to get the router, then use router.push() to navigate.
Fill all three blanks to create a component that navigates to a dynamic page '/profile/[id]' with id '123'.
function ProfileButton() {
const router = [1]();
const userId = '123';
return (
<button onClick={() => router.[2](`/profile/$[3]`)}>
Go to Profile
</button>
);
}Use useRouter() to get the router, router.push() to navigate, and pass the userId variable in the URL.