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

Codevolution
26 Feb 202408:21

Summary

TLDRこのビデオでは、Reactサーバーコンポーネントを活用して、サーバーコンポーネントとクライアントコンポーネントの両方を作成する実践的なプロセスを紹介しています。Next.jsアプリケーションでの具体的な手順を説明し、新しい「rendering-demo」プロジェクトを作成してから、サーバーコンポーネントとしての「Aboutページ」の追加、そしてクライアントコンポーネントとしての「Dashboardページ」の作成までを解説します。特に、「use client」ディレクティブを使うことでクライアントコンポーネントを定義し、ブラウザAPIへのフルアクセスとインタラクティビティのハンドリングが可能になる点を強調しています。ReactとNext.jsを使ったサーバーとクライアントのコンポーネントモデルの理解と実装方法を、初心者にもわかりやすく説明しています。

Takeaways

  • 😀 By default, every component in a Next.js app is a server component, including layout and pages.
  • 📝 Use the `useClient` directive to define client components in Next.js.
  • 💻 Server components run only on the server, while client components run on client and server.
  • 🎯 Client components can access browser APIs and handle interactivity.
  • 🔄 Client components pre-render once on server to quickly show HTML.
  • 😵 Server components cannot use state or interact with browser APIs.
  • 📑 Next.js optimizes by pre-rendering client components on server.
  • ✅ Add `useClient` directive to allow component to access browser APIs.
  • 📦 Server components have benefits like 0 bundle size, security, SEO.
  • ⬇️ Use `useClient` to cross from server to client component boundary.

Q & A

  • Next.jsアプリケーションでサーバーコンポーネントとクライアントコンポーネントを作成する際の最初のステップは何ですか?

    -最初のステップは、`npx create-next-app` コマンドを使用して新しいNext.jsプロジェクトを作成することです。

  • Next.jsアプリケーションにおけるすべてのコンポーネントのデフォルトの種類は何ですか?

    -デフォルトでは、すべてのコンポーネントはサーバーコンポーネントとして扱われます。

  • 新しい「about」ページをNext.jsアプリケーションに追加するためにどのフォルダにファイルを作成しますか?

    -`about` フォルダ内の `app` フォルダに `page.tsx` ファイルを作成します。

  • サーバーコンポーネントがサーバー上で実行されることを確認する方法は何ですか?

    -ブラウザのコンソールではなく、アプリケーションを実行しているターミナルでログメッセージを確認します。

  • サーバーコンポーネントにおいて `useState` フックの使用を試みた結果は何ですか?

    -`useState` はクライアントコンポーネント環境を期待しているため、エラーが発生します。

  • クライアントコンポーネントを作成するために特別な指示(ディレクティブ)をファイルのトップに含める必要があるのはなぜですか?

    -`use client` ディレクティブは、そのコンポーネントがクライアントサイドで実行されることを指定し、ブラウザAPIへのフルアクセスとインタラクティビティの処理を可能にします。

  • クライアントコンポーネントがサーバー上で一度レンダリングされる理由は何ですか?

    -クライアントコンポーネントがサーバー上で一度レンダリングされるのは、ユーザーがページのHTMLコンテンツを即座に見ることができ、空白の画面ではなく最適化戦略として実装されています。

  • Next.jsアプリケーションでサーバーコンポーネントとクライアントコンポーネントを区別するために使用するキーとなるディレクティブは何ですか?

    -クライアントコンポーネントを定義するために `use client` ディレクティブを使用します。

  • Next.jsにおけるサーバーコンポーネントの利点は何ですか?

    -サーバーコンポーネントの利点には、ゼロバンドルサイズ、サーバーサイドリソースへのアクセス、強化されたセキュリティ、より良いSEOなどがあります。

  • Next.jsアプリケーションにおいて、クライアントコンポーネントのレンダリング挙動について説明してください。

    -クライアントコンポーネントは主にクライアントで実行され、ブラウザAPIへのアクセスが可能ですが、ユーザーがページのHTMLコンテンツをすぐに見ることができるように、サーバー上で一度プレレンダリングされます。

Outlines

00:00

🔨 Reactサーバーコンポーネントとクライアントコンポーネントの実践

このビデオでは、Reactのサーバーコンポーネントとクライアントコンポーネントの実践的な作成方法を説明します。最初に、npx create-next-appを使用して新しいNext.jsプロジェクトを作成し、その後、Next.js(nxsと表記されることがある)アプリケーションでのReact Server Components(RSC)アーキテクチャの統合について解説します。Next.jsでは、デフォルトで全てのコンポーネントがサーバーコンポーネントとみなされます。これには、新規プロジェクトにデフォルトで含まれるルートレイアウトとルートページも含まれます。具体的な例として、「about」ページの追加方法を説明し、サーバーコンポーネントがブラウザAPIやユーザーのインタラクティビティと直接的に対話できない制限を持つこと、そしてクライアントコンポーネントに必要な特別な指示(use clientディレクティブ)の使用方法についても触れます。

05:01

🔄 クライアントコンポーネントのレンダリングと挙動の理解

このセクションでは、Next.jsにおけるクライアントコンポーネントのレンダリング挙動に焦点を当てます。特に、クライアントコンポーネントがブラウザ上でのインタラクティビティを持つ一方で、最適化のためにサーバー上で一度プレレンダリングされることを説明します。これにより、ユーザーがページのHTMLコンテンツを即座に見ることができるようになります。また、Next.jsの「use client」ディレクティブを使用してクライアントコンポーネントを明示的に指定する方法、およびReactサーバーコンポーネントのアーキテクチャにおいて、サーバーコンポーネントとクライアントコンポーネントがどのように異なり、どのように連携するかについて深く掘り下げます。このビデオは、サーバーとクライアントの両方のコンポーネントをNext.jsアプリケーションで定義し、レンダリングする方法についての理解を深めることを目指しています。

Mindmap

Keywords

💡server components

Components that are rendered on the server side. They have benefits like faster page loads, better SEO, but can't use browser APIs or state.

💡client components

Components that are rendered on the client side and can use browser APIs and state. They also get pre-rendered on the server for faster initial page loads.

💡next.js

A React framework that enables server side rendering and static site generation. It handles setting up server and client side components.

💡useClient directive

A directive in Next.js that marks a component as a client component so it gets rendered on the client side.

💡rendering

The process of generating HTML from a component. Server components render on the server while client components render on both server and client.

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, better SEO

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

Use the `useClient` directive to define client components in Next.js

The `useClient` directive signals that a component is intended for client-side execution

Client components gain access to browser APIs and can handle interactivity

Client components execute once on the server and then on the client

Client component rendering behavior can be confusing due to server-side pre-rendering

Server components only render on the server

Client components render once on server for initial HTML, then fully client-side

Use `useClient` directive to define client components in Next.js

Server components are rendered only on server

Client components render on server for initial HTML, then client-side

Client components have access to browser APIs and interactivity

Next video covers rendering lifecycle differences of server & client components

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

Rate This

5.0 / 5 (0 votes)

Benötigen Sie eine Zusammenfassung auf Englisch?