Next.js routing
Next.js has a filesystem-based router where every page file represents a page. The pages are special files that exist in the pages
folder, and they have the following structure:
const Page = () => { return <div>Welcome to the page!</div> } export default Page;
As you can see, only exporting the page
component as a default export is required; this is the minimum requirement for a page to be defined. We will see what else can be exported from a page in a few moments.
Since the routing is filesystem-based, routes are determined by how the page files are named. For example, the page pointing to the root route should be defined in the src/pages/index.tsx
file. If we want the about page, we can define it in src/pages/about.tsx
.
For any complex application with dynamic data, it is not enough to only create predefined pages. For example, let’s say we have a social network application where we can visit user profiles...