正在加载视频...

视频加载失败

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 次观看 • 3 年前 •via X (Twitter)

10 条评论

Armando⚡️ 的头像
Armando⚡️3 年前

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 的头像
Alex Sidorenko3 年前

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 🦋 的头像
Fredkisss is on 🦋 blue sky 🦋3 年前

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 的头像
Nithur3 年前

It would be cool if we can generate these pages statically

Alex Sidorenko 的头像
Alex Sidorenko3 年前

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 的头像
Ben Gibb3 年前

@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 的头像
Alex Sidorenko3 年前

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 的头像
Armo3 年前

Thanks for creating amazing content!

Hemanta Sundaray 的头像
Hemanta Sundaray3 年前

Make some videos on Next js middlewares.

Alex Sidorenko 的头像
Alex Sidorenko3 年前

Do you have any specific cases in mind?

相关视频

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 次观看 • 2 年前