Loading video...

Video Failed to Load

Go Home

How to refactor an existing client component into a server component? 1. Move the state to the URL 2. Move data fetching to the server 3. Isolate interactivity into a smaller client component

112,401 views • 3 years ago •via X (Twitter)

10 Comments

Armando⚡️'s profile picture
Armando⚡️3 years ago

You are killing it, lot of things I learned seeing your videos, will be nice to have a web page that renders all these tips so I don’t have to bookmark them 😅, anyways awesome content!

Alex Sidorenko's profile picture
Alex Sidorenko3 years ago

Thanks, I am working on the dedicated page. *Also secretly hoping that @X will implement better UI for tweet collections so I don't have to build anything

Fredkisss is on 🦋 blue sky 🦋's profile picture
Fredkisss is on 🦋 blue sky 🦋3 years ago

Now what is missing is adding Suspense to show loading states on the server, or use useTransition (in the Search client component) to show loading state on the client.

Nithur's profile picture
Nithur3 years ago

It would be cool if we can generate these pages statically

Alex Sidorenko's profile picture
Alex Sidorenko3 years ago

We can. 1. Substitute query params with dynamic routes. So the route looks like this /movies/doctor instead of /movies?search=doctor 2. Pass most popular search requests to generateStaticParams

Ben Gibb's profile picture
Ben Gibb3 years ago

@asidorenko_ pardon the noob but sincere question, but what is the purpose of doing this? How is the code better in moving to the server?

Alex Sidorenko's profile picture
Alex Sidorenko3 years ago

Good question, nothing noob about it. - Ship less JS to the client (improves time to interactive) - Can use secret keys when fetching data - Less client-side re-rendering - Can pre-generate popular search queries at build time - State in the URL is often better UX

Armo's profile picture
Armo3 years ago

Thanks for creating amazing content!

Hemanta Sundaray's profile picture
Hemanta Sundaray3 years ago

Make some videos on Next js middlewares.

Alex Sidorenko's profile picture
Alex Sidorenko3 years ago

Do you have any specific cases in mind?

Related Videos

How should you search, filter, and paginate data with Next.js? This demo has 50,000 books in a Postgres database. • Page Load: When the page loads, we see the React Suspense fallback. This loading skeleton is displayed until the first page of books is retrieved from the database. • Searching: The search input has a 200ms debounce. After 200ms of inactivity, the form submits, updating the URL state with `?q={search}`. The Server Component reads `searchParams` and queries the database. On form submission, a React transition starts, allowing us to read the pending status with `useFormStatus` to display an inline loading state. • State Preservation: Navigating to an individual book page retains the search input state. Reloading the page or sharing the link preserves the search results. • Client-side Filtering: Filtering authors in the left sidebar is done client-side. Authors are fetched by a Server Component and passed as props to the sidebar. Changing the input value updates React state and re-renders the sidebar. • Optimistic Updates: The sidebar’s selected authors are optimistically updated with `useOptimistic`. Checkbox selections update instantly without waiting for the URL to change. • State Preservation: Navigating to an individual book page retains the sidebar filter input and selected author state across navigations, giving it an app-like feel. • Pagination: Navigating between pages updates the URL state, triggering the Server Component to query the database for the specific page of books. We also fetch the total book count to show the total number of pages. This demo isn't perfect yet (still working on it) but it's been a fun playground for some of these patterns. You can imagine a similar experience for thousands of movies, cars, products, or any other very large dataset. Demo → Code →

Lee Robinson

236,802 views • 2 years ago