Next.js 14 Tutorial - 50 - Server and Client Components

Codevolution
26 Feb 202408:21

Summary

TLDRThe video explains the difference between server and client components in Next.js applications. By default, all components in Next.js are server components, which provide benefits like zero bundle size and SEO. However, server components can't use browser APIs or handle interactivity. The video shows how to create a client component using the 'useClient' directive, allowing access to state and browser APIs. It also covers the rendering behavior - client components prerender on the server then execute on client. The video summarizes that server components only run on server, while client components run on both server and client.

Takeaways

  • πŸ˜€ By default, every component in a Next.js app is a server component, including the default pages
  • πŸ‘ Server components provide benefits like zero bundle size, access to server resources, and better SEO
  • ⏱ Server components cannot interact with browser APIs or handle interactivity like state
  • πŸ’‘ Use the `useClient` directive to define client components that can use browser APIs and state
  • πŸ“Ί Client components render once on the server for pre-rendering, and then client-side for interactivity
  • 😡 Server components only render on the server, not on the client
  • πŸ”„ Client components render on server for pre-rendering, then client for interactivity
  • βš™οΈ The `useClient` directive signals Next.js to execute a component client-side
  • πŸƒβ€β™‚οΈ Client components can access browser APIs and handle interactivity
  • πŸ’‘ Understanding server vs client components is key to using React Server Components

Q & A

  • What is the default component type in a Next.js application?

    -By default, every component in a Next.js app is considered a server component, including the root layout and root page components.

  • How do you create a client component in Next.js?

    -To create a client component in Next.js, you must include the `useClient` directive at the top of the component file. This signals to Next.js that the component is intended for client-side execution.

  • Can server components use state and interact with browser APIs?

    -No, server components cannot use state or interact directly with browser APIs because they are rendered on the server where there is no browser.

  • Where do client components execute?

    -Client components execute primarily on the client-side to allow access to browser APIs and interactivity. However, they are also pre-rendered once on the server to allow immediate display of HTML without a blank screen.

  • What are some benefits of server components?

    -Benefits of server components include zero bundle size, access to server-side resources, enhanced security, better SEO, etc.

  • When will a client component log a message?

    -A client component will log a message in the browser console when rendered on the client-side. It will also log a message in the terminal when pre-rendered on the server.

  • Why can't a server component use React hooks like useState?

    -Server components run on the server where there is no concept of state that exists in the browser. React hooks like useState require a client-side environment to work properly.

  • What is the purpose of pre-rendering client components on the server?

    -Pre-rendering client components on the server allows the user to immediately see the page's HTML content rather than a blank screen while waiting for client-side JavaScript.

  • How do you define routes for server and client components?

    -Routes for both server and client components are defined the same way in Next.js - by creating files under the pages directory. The component type is determined by whether the `useClient` directive is present.

  • Can a single Next.js application have both server and client components?

    -Yes, a Next.js application can have a mix of server components and client components defined using the `useClient` directive.

Outlines

00:00

πŸ“Ί Creating and Using Server and Client Components in Next.js

The first paragraph introduces server and client components in Next.js. It explains that by default, all components in a Next.js app are server components, including layout and page components. It then demonstrates creating a simple About page server component and shows how it runs on the server. It explains limitations of server components in not being able to use browser APIs or handle interactivity. It then walks through creating a Dashboard page client component using the useClient directive, useState hook, and shows how it can access browser APIs and manage state.

05:01

πŸ–₯️ Client Component Rendering Behavior and Lifecycle

The second paragraph dives deeper into client component rendering behavior. It shows that client components render once on the server for pre-rendering and optimization, and then mount on the client for interactivity. It reiterates that server components only render on the server, while client components render on both server and client. It summarizes the key difference between server and client components in Next.js and their rendering lifecycle.

Mindmap

The video is abnormal, and we are working hard to fix it.
Please replace the link and try again.

Keywords

πŸ’‘server components

Components that run on the server side. They bring benefits like zero bundle size, access to server resources, better SEO. But they can't interact with browser APIs or handle interactivity. All components in Next.js are server components by default.

πŸ’‘client components

Components that run on the client side and can access browser APIs and handle interactivity. They must include the 'useClient' directive. Client components also prerender on the server first to allow immediate display of HTML.

πŸ’‘useClient directive

A special instruction that signals Next.js to define a component as a client component instead of default server component. It allows access to browser APIs.

πŸ’‘rendering

The process of generating and displaying output from a component. Core theme of video is rendering behaviors of server vs client components.

πŸ’‘rehydration

The process of taking prerendered HTML from server and attaching event listeners on client to make it interactive.

πŸ’‘prerendering

The initial one time rendering of a client component on the server side. Allows component HTML to load immediately.

πŸ’‘browser APIs

Interfaces provided by web browsers to interact with them programmatically (e.g. DOM). Server components cannot access these.

πŸ’‘interactivity

Ability for users to interact with and manipulate components. Handled by client components.

πŸ’‘state

A variable that allows tracking component data over renders. Server components do not have access to React state.

πŸ’‘optimization

Improving performance. Prerendering client components is an optimization to allow faster first render.

Highlights

By default every component in a Next.js app is considered a server component

Server components run on the server, bringing benefits like zero bundle size, access to server resources, security, and SEO

Server components can't interact with browser APIs or handle user interactivity

To create client components, use the "use client" directive to signal they are for client-side execution

Client components gain access to browser APIs and interactivity

Client components render once on the server for pre-rendering, and then on the client

Server components only render on the server, client components render on server and client

Client component name is confusing - they execute once on server but focus is client rendering

In React Server Components, router components default to server, use "use client" for client ones

Server components provide SEO, security, performance

Client components enable interactivity

Client components prerender on server before client-side hydration

Understand server vs client component rendering lifecycles in Next.js

Like, subscribe, and turn on notifications for more content

See you in the next video!

Transcripts

play00:00

in our previous video we dived into

play00:02

react server components which introduced

play00:04

a dual component model differentiating

play00:06

between server components and client

play00:08

components in this video we will put

play00:11

Theory into practice by creating both

play00:14

types of components in an xjs

play00:17

application for this section on

play00:19

rendering I have created a new nexs

play00:22

project using the command npx create

play00:25

next app at lat test followed by the

play00:27

project name which is rendering hyphen

play00:29

demo

play00:30

once the command is executed you should

play00:33

have a project similar to

play00:35

mine now diving into the RSC

play00:37

architecture and its integration with

play00:40

nxs it is important to note that by

play00:43

default every component in a NS app is

play00:46

considered a server

play00:48

component this includes the root layout

play00:51

and the root page that come out of the

play00:54

box with a new next year project let's

play00:57

create a new component to verify this

play01:00

suppose we want to add a new about page

play01:03

to our application we would create a new

play01:06

file called page. TSX within the about

play01:11

folder within the app

play01:13

folder let's populate it with a basic

play01:16

react

play01:18

component I will use the pieces Explorer

play01:21

extension for the code

play01:23

snippet we're going to insert a next

play01:25

shes page

play01:28

component export default function

play01:32

page let's call this about

play01:35

page and return an H1 tag that says

play01:38

about

play01:39

page just by doing this you have created

play01:42

a server component let's add a lock

play01:45

statement to verify this

play01:48

console.log about server

play01:51

component if we head to the

play01:54

browser and navigate to localhost 3000

play01:58

slab we don't see any log message in the

play02:02

browser

play02:03

console instead we see the message in

play02:07

the terminal where we are running our

play02:10

application it is clear our component is

play02:12

a server component this component runs

play02:15

on the server bringing along all the

play02:18

benefits of server components such as

play02:20

zero bundle size access to serers side

play02:22

resources enhanced security better SEO

play02:27

Etc but several components have their

play02:29

limitation s they can't interact

play02:31

directly with browser apis or handle

play02:34

user

play02:35

interactivity if we try to incorporate

play02:37

some State into our about page using use

play02:40

state for

play02:42

example name set name with a default

play02:45

value of an empty string while making

play02:48

sure we import you state at the

play02:51

top head back to the

play02:54

browser you will see an error this is

play02:58

because you State expects a client

play03:01

component environment but our about

play03:03

Peach is a server component if you think

play03:06

about it server components cannot use

play03:08

State because they're rendered on the

play03:10

server where there is no concept of

play03:13

State as it exists in the browser this

play03:16

again confirms the fact that every

play03:19

component created in XS is a server

play03:22

component unless specified

play03:24

otherwise let's leave the about page as

play03:27

a functioning server component

play03:31

and learn how to create a new client

play03:34

component we'll make use of a new file

play03:37

so within the app

play03:39

folder dashboard page.

play03:43

TSX and Export a simple component that

play03:46

uses state to manage a user's name once

play03:49

again from pieces Explorer I'm going to

play03:52

insert an nexs page component change the

play03:56

name to dashboard page import us State

play04:01

create a state variable called name with

play04:03

an initial value of empty

play04:05

string and for the jsx we add an input

play04:09

element whose value is equal to the name

play04:11

State variable and on change we call set

play04:14

name passing in the input value we then

play04:18

have a paragraph element that renders

play04:20

hello followed by the

play04:22

name now at the top of this file we must

play04:26

include a directive or to put it simply

play04:29

a special instruction within the code

play04:32

within double quotes use

play04:34

client this directive acts as our ticket

play04:38

to cross the boundary from server to

play04:40

client side and is what allows us to

play04:43

Define client

play04:44

components it signals to nexas that this

play04:48

component dashboard page along with any

play04:51

components it Imports is intended for

play04:54

client side

play04:56

execution as a result the component

play04:58

gains full access to browser apis and

play05:01

the ability to handle

play05:03

interactivity if we return to the

play05:06

browser and navigate to SL

play05:10

dashboard we see the component with its

play05:13

state working as

play05:15

expected we do have the text but the

play05:18

styling is not

play05:20

appropriate let's now shift our

play05:22

attention to a very important point

play05:25

about client components rendering

play05:27

behavior let's add a lock statement

play05:30

within the dashboard

play05:33

component dashboard client component in

play05:37

the home

play05:39

page let's add a link to navigate to the

play05:42

dashboard page so at the top import link

play05:47

from next SL link and after the

play05:51

paragraph tag I'm going to add the link

play05:54

component the text is

play05:57

dashboard and each ref is equal to slash

play06:04

dashboard back in the

play06:06

browser if we navigate to localhost

play06:11

3000 we should see the dashboard link

play06:14

click on the

play06:16

link and we see the log message in the

play06:19

browser's

play06:21

console we have it twice because of

play06:24

reacts strict mode if we go back to the

play06:28

terminal you can see there is no log

play06:30

message about the dashboard component

play06:33

rendering however if you were to reload

play06:36

the dashboard

play06:38

route you can see we have the message in

play06:41

the browser's console again but if we

play06:44

head back to the

play06:46

terminal we see the same message here as

play06:49

well as I mentioned in the previous

play06:52

video client components are primarily

play06:55

executed in the client and have access

play06:57

to browser apis but they are also also

play07:00

pre-rendered once on the server to allow

play07:02

the user to immediately see the Page's

play07:04

HTML content rather than a blank screen

play07:08

it is an optimization strategy that

play07:10

nextjs implements and is recommended by

play07:13

react the name client component is

play07:16

indeed confusing as a client component

play07:19

executes once on the server but I'm

play07:21

confident it will not get in your way of

play07:23

learning react server

play07:25

components a very important point to

play07:27

keep in mind

play07:30

to summarize in the react server

play07:32

component architecture and by extension

play07:35

in the nextjs app router components are

play07:38

server components by default to use

play07:41

client components you must include the

play07:43

use client directive at the top server

play07:46

components are rendered only on the

play07:48

server whereas client components are

play07:50

rendered once on the server and then on

play07:52

the

play07:54

client now that we understand how to

play07:56

define both server and client components

play07:59

in next shares in the next video Let's

play08:01

dive into the rendering life cycle of

play08:04

server and client components in next

play08:06

shares if you're enjoying the content

play08:08

please hit the like button subscribe and

play08:11

turn on notifications to stay updated

play08:13

I'll see you in the next

play08:19

video