0% found this document useful (0 votes)
19 views9 pages

Next.js Rendering and Routing Guide

This document contains code to create routes in Next Js Application

Uploaded by

soultune.999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

Next.js Rendering and Routing Guide

This document contains code to create routes in Next Js Application

Uploaded by

soultune.999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Rendering

Rendering converts the code you write into user interfaces. React and [Link] allow you to create
hybrid web applications where parts of your code can be rendered on the server or the client.

Rendering Environments
There are two environments where web applications can be rendered: the client and the server.

The client refers to the browser on a user's device that sends a request to a server for your
application code. It then turns the response from the server into a user interface.

The server refers to the computer in a data center that stores your application code, receives
requests from a client, and sends back an appropriate response.

Basic Routing
The Pages Router has a file-system based router built on concepts of pages. When a file is added
to the pages directory it's automatically available as a route.

Profile Page:

 Click on src\app -> New Folder


 Name the folder as profile
 Now click on profile folder -> New File
 Name the file as [Link]
 Write the code of profile page in this [Link] file
Src\app\Profile\[Link]

export default function Profile(){


return(
<h1> Profile Page</h1>
);
}

Localhost:3000/profile

Similarly create contact page.

Linking and Navigation

Linking:
Create link in home page to move from home page to profile page and contact page.

Open main [Link] file and import Link


i.e.

import Link from "next/link";


[Link]

import Link from "next/link";


export default function Home() {

return (
<main>
<h1>Basic Routing</h1>

<Link href="/profile"> Go to Profile Page</Link>


</main>
);
}

Click on Go to Profile Page link


import Link from "next/link";
export default function Home() {

return (
<main>
<h1>Basic Routing</h1>

<Link href="/profile"> Go to Profile Page</Link>

<Link style={{display:'block'}} href="/contact"> Go


to Contact Page</Link>

</main>
);
}

Now to move from profile page to home page follow same steps

Profile/[Link]

import Link from "next/link";


export default function Profile()
{
return(
<main>
<h1> Profile Page</h1>
<Link href="/"> Go to Home Page</Link>
</main>
);
}

Navigation:
Now create button in contact page to move from contact page to profile page.

Contact/[Link]

"use client"
import { useRouter } from "next/navigation";
export default function Contact()
{
const router = useRouter();
const move=(name)=>{
[Link](name)
}

return(
<main>
<h1> Contact Page</h1>

<button onClick={()=>move("/profile")}> Go to Profile


Page </button>
</main>
);
}

Click on button
Contact/[Link]

Nested Routes
To create a nested route, you can nest folders inside each other.

Normal Routing Nested Routing


 Home  Home
 Profile  Profile
 Login - Profile Overview
 Contact - Profile Setting
 Contact
- Email
- Phone

Src/app/profile/overview/[Link]
Src/app/profile/[Link]
Localhost:3000/profile/overview
Localhost:3000/profile

Common questions

Powered by AI

Server-side rendering (SSR) in Next.js for initial page load offers significant advantages, particularly concerning SEO. SSR provides pre-rendered HTML content when the initial request is made, ensuring that search engines can easily index the page without executing JavaScript, which improves SEO visibility. Additionally, SSR often results in faster perceived loading times for users, as they immediately see the pre-rendered content. This initial improvement in load performance can increase user retention and engagement, significantly impacting the web application's effectiveness .

In a basic routing setup using Next.js, navigation from the Profile page back to the Home page can be implemented by importing the Link component from 'next/link' within the Profile function file (Profile/Page.js). You then create a link element that navigates to '/' for the Home page, such as <Link href='/'> Go to Home Page</Link>. This link provides client-side navigation, allowing users to return to the home page smoothly without reloading .

Rendering on the client occurs in the user's browser, which involves sending a request to a server for application code that is then processed and turned into a user interface within the browser. In contrast, server-side rendering happens on a computer within a data center that holds the application code. Here, the server generates the HTML and sends it back to the client where the browser can directly display the content without further processing. Client-side rendering typically enhances interactive user experiences but can have initial load performance drawbacks, while server-side rendering can improve load times as it pre-generates page content .

The useRouter hook provides programmatic navigation within a Next.js application, allowing for dynamic interactions such as navigating to a route based on user actions. It is imported from 'next/navigation' and is particularly useful for functional components, allowing navigation without JSX links or buttons linked to clicks. On the other hand, the Link component is utilized for static navigation, embedding into the JSX to create hyperlink-like behavior that manages history and state. The useRouter hook provides more flexible and context-sensitive navigation control compared to the more declarative Link component .

In Next.js, a basic route is created using a file-system based router. You start by creating a new folder and then a file within the pages directory. For instance, to create a 'Profile' page, you create a folder named 'profile' in src/app, add a new file named page.js, and write the profile page code in that file. This automatically makes the page accessible through the route localhost:3000/profile .

In Next.js, nested routes are created by nesting folders within the pages directory to represent hierarchy. For example, to create a nested route under 'profile', you create a directory structure such as src/app/profile/overview/page.js. The route localhost:3000/profile/overview will map to this structure. This allows for organizing different levels of user interface content under a main category, providing clear paths to access specific functionalities like 'Profile Overview' and 'Profile Setting' .

Linking and navigation are essential for providing a seamless user experience by allowing users to move between different parts of a web application without reloading the entire page. In Next.js, this is implemented by using the Link component. You import Link from 'next/link' and create link elements to navigate between routes. For example, in the Home component, you use <Link href='/profile'> to navigate to the Profile page. This client-side navigation is efficient and helps maintain the application's state .

The Pages Router in Next.js is inherently tied to the concept of file-system based routing, as it automatically maps page files within the pages directory to routes without additional configuration. Each JavaScript file becomes a new route, where the directory path determines the URL path structure. This relationship simplifies route setup by eliminating the need to manually define every route, allowing developers to organize application content structurally in code and rendering a more intuitive routing approach compared to traditional route definitions .

The 'use client' directive in a Next.js component signifies that the component should be rendered on the client side, essential for features that require direct DOM interactions, such as hooks and state. In the context of programmatic navigation using the useRouter hook, 'use client' ensures that the component can utilize the browser's navigation functions dynamically, such as using useRouter to push new routes based on events like button clicks. This provides the capability to navigate users without relying on predefined links .

Hybrid web applications can leverage both client-side and server-side rendering, optimizing user experience and performance. React and Next.js allow developers to choose the rendering environment based on the use case, optimizing loading times and application responsiveness. For instance, initial page loads can utilize server-side rendering to quickly display content, while post-load interactions benefit from client-side rendering to maintain a dynamic and fluid user interface without additional server requests. This flexibility allows efficient resource usage and improved SEO aspects due to server-rendered content .

You might also like