Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Peter Jacxsens
Peter Jacxsens

Posted on • Edited on

     

2./ Client and server components in Next 13

It's all about performance.React used to be client side only. This meant browsers had to download the entire bundle and thenReact would render all the components.

React andNext constantly improved their working by adding things like server-side rendering and data fetching, prerendering and many more. Server components are the next step in this evolution. Why? Better performance.

Client components

Client components are components that:

  1. Add interactivity and event listeners (onChange,onClick, ...)
  2. Use hooks:
    • State or lifecycle hooks (useState,useEffect, ...)
    • Custom hooks that use state or lifecycle hooks.
    • TheuseSearchParams hook. (later chapter on this)
    • Other hooks likeuseRouter orusePathname.
  3. Use browser-only APIs (localStorage,GeoLocation,...)
  4. Use Class components.

How do you use a client component?

A component becomes a client component when it uses theuse client directive:

'use client';exportdefaultfunctionMyComponent(){return<>hello</>;}
Enter fullscreen modeExit fullscreen mode

We mentioned above that you have to use client components when your components needs interactivity or event listeners, certain hooks, browser only API's or is a Class component. Writing a component that fulfils these conditions without theuse client directive makesNext throw an error.

But, theuse client directive is what makes a component a client component, not the use of f.e. event listeners oruseState(). These only require you to use theuse client directive. This means that you can make any component a client component just by using this directive. There is no reason to do this (apart from testing like we will do in this series) but just know this nuance.

There is one other case where a component becomes a client component without using the directive. When you import said component into a client component. More on this later on.

What a client component is NOT

Client components are used when your component has certain requirements (interactivity and event listeners, certain hooks, browser only API's and class components).

Client components are not equal to client-side rendered components!!!! Reread the previous paragraph. When your components have certain requirements, you make them into client components. It does not mention anything about them being client-side rendered.

Client components in React vs Next

Client components in pureReact (noNext) are client-side rendered. Client components inNext on the other hand are mostly rendered server-side ... and a bit on the client. Why, because it improves performance.

IfNext renders client components server-side, does that mean that they are server components? No!

  1. Server components render ONLY on the server, never on the client.
  2. The render process of client components is different. How it differs is not relevant but the result is that client components will render mostly on the server and then a bit on the client.

I know, this doesn't fully make sense but we are going to come back to this a lot. For now, just remember:

  1. Client component = component that has certain requirements.
  2. Client component !== client-side rendered component.
  3. Client components will render mostly on the server and a bit on the client.

Server components

Every component that is not a client component is by default a server component in the newapp router. If your route is defined in the/app folder, then every component in this route is either a server component or a client component. In the oldpages router, all components are client components. Server components only exist in theapp router.

Server components are also defined by the fact that they are rendered on the server only. They never render on the client. (Client components run both server and client-side.)

Why server components

Server Components allow developers to better leverage server infrastructure. For example, you can move data fetching to the server, closer to your database, and keep large dependencies that previously would impact the client JavaScript bundle size on the server, leading to improved performance. Server Components make writing a React application feel similar to PHP or Ruby on Rails, but with the power and flexibility of React and the components model for templating UI.

With Server Components, the initial page load is faster, and the client-side JavaScript bundle size is reduced. The base client-side runtime is cacheable and predictable in size, and does not increase as your application grows.

...

Next docs

A server component is best suited to:

  • Fetch data.
  • Access backend resources (directly)
  • Keep sensitive information on the server (access tokens, API keys, etc)
  • Keep large dependencies on the server / Reduce client-side JavaScript

Next docs

In short, server components are better for performance and that is whyNext made them the default (inapp router). Always use server components, except when you have to use client components due to certain requirements (interactivity and event listeners, certain hooks, browser only API's and class components).

Conclusion

Next 13 introduced server components. A new way to optimize your projects. Server components are the default in theapp router and should be used for everything except when your component needs:

  1. Interactivity and event listeners.
  2. Certain hooks: state and lifecycle hooks, custom hooks that use state of lifecycle hooks, theuseSearchParams hook and other hooks likeuseRouter orusePathname.
  3. Browser only api's.
  4. React class components.

In these cases you use client components.

Client components can be a bit confusing because inNext they are rarely client-side rendered.Next optimizes all components (client and server) by prerendering them server-side.

  • Client components aren't fully server-side rendered. They also render partly client-side.
  • Server components never render client-side, only server-side.

This was a theoretic explanation of client and server components. I would love to make this more concrete with examples but we need to look into static and dynamic rendering first as these impact client and server components.

Next, part 3:Static and dynamic rendering in Next 13

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
nghihua profile image
nghihua
A CS sophomore trying to navigate
  • Joined

really like the differentiation between client components and client-side rendered components!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Front-end developer
  • Location
    Belgium
  • Education
    self taught
  • Joined

More fromPeter Jacxsens

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp