Next.js 13+ introduces Intercepting Routes, a feature that allows developers to override navigation behavior dynamically. This is useful for implementing modal routes, side panels, or preview pages without changing the core navigation flow.
Intercepting Routes lets you load a route inside an existing layout instead of navigating to a completely new page. This is achieved using the (..) folder convention in the app directory.
Create a regular page inside the app director
Export default function ProfilePage() {
Return <h1>User Profile</h1>;
}
This will be available at /profile.
To display /profile as a modal within another page (e.g., /dashboard), create an intercepting route inside
app/dashboard/(..)/profile/page.tsx:
export default function ProfileModal(){
Return (
<div className=”modal”>
<h1>User Profile (Modal)</h1>
</div>
)
}
Now, navigating to /dashboard/profile will show the modal inside the dashboard instead of navigating away.
Intercepting Routes in Next.js provides a flexible way to handle navigation, allowing partial updates without breaking the user experience. Use them for modals, previews, or sidebars to enhance app interactivity!
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our ReactJS Expertise.