Next.js Discord

Discord Forum

Understanding How Next.JS Render Client Component

Unanswered
Romanian Mioritic Shepherd Dog posted this in #help-forum
Open in Discord
Romanian Mioritic Shepherd DogOP
This is a quick and small question: it says that client components still get SSR'd, right?

https://nextjs.org/docs/app/building-your-application/rendering/client-components#full-page-load

Specifically, during a full-page load, it states:
"Next.js will use React's APIs to render a static HTML preview on the server for both Client and Server Components."

So, when rendering the static HTML, does that mean the server understands and can run useState?

Because from this code alone:

import { useState } from 'react';
 
export default function LikeButton() {
  const [likes, setLikes] = useState(11111);
 
  function handleClick() {
    setLikes(likes + 1);
  }
 
  return <button onClick={handleClick}>Like ({likes})</button>;
}


The initial value of likes is 11111. Why is the initial value displayed on the screen 11111 (when doing a full refresh page reload)? Doesn't that mean that when Next.js renders the static HTML on the server, it understands useState? I expected it to be 0 at first (Because server runtime does not understand useState, it uses the default value of a number variable) and then flash to 11111 when JavaScript hydrates the page.

8 Replies

Romanian Mioritic Shepherd DogOP
pre-rendered? So we are expecting a static html for that route's page.tsx?
Because I don't see any static html for the route
I thought clinet component got SSR'd and not SSG'ed
(mind you, I'm very new to Next.JS and I am not a native english speaker, so maybe I'm missing something here)
"pre-render" is a broader term to the initial render pass that happens on the server, it can be used for SSR or SSG
so normally when you request a SSR page, react will do an initial pass on every component and return their initial state to the client
Romanian Mioritic Shepherd DogOP
Thank you!