Menu

How Routing Works in Next.js: Pages, Layouts and Links Explained
πŸ“°

How Routing Works in Next.js: Pages, Layouts and Links Explained

DEV Community: webdevΒ·Lavkush kumarΒ·10 days ago
#3jXYtXWb
#devtowebdev#devto#dev#folder#file
Reading 0:00
15s threshold

Originally published on CodingCush .

In Next.js, routing is based on your folder structure instead of a configuration file: every folder inside the app directory becomes part of the URL, a file called page.tsx inside that folder becomes the page visitors see, and a file called layout.tsx wraps pages with shared parts such as a header and footer. You do not install a router or register routes β€” you simply create folders and files, and Next.js turns them into web addresses.

Routing Without a Router Library

If you have used plain React, this feels pleasantly strange. In a React single-page app you normally install something like React Router and then write route definitions by hand: this path shows this component, that path shows that one. Forget a route, and your page exists but nobody can reach it β€” a little like building a room and forgetting the door.

Next.js took the opposite approach, called file-system routing . The framework watches your app folder and builds the route table for you.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.