Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hands-on React Server Components, Server Functi...

Aurora Scharff
September 16, 2024
41

Hands-on React Server Components, Server Functions, and Forms in the Next.js App Router

In this workshop, we will explore React's latest features: Server Components, Server Functions, and Forms. Discover how to optimize server-side rendering, enhance application interactivity with Server Actions and multiple new React 19 hooks, and create robust forms for efficient data handling and validation.

Designed for developers of all levels, this workshop provides practical skills to build scalable, performant web applications.

Aurora Scharff

September 16, 2024
Tweet

Transcript

  1. Hands-on React Server Components, Server Functions, and Forms in the

    Next.js App Router Aurora Scharff @aurorascharff
  2. Introduction > Aurora Scharff > Norway, Oslo > Consultant at

    Inmeta > Microsoft MVP in web dev > Active RSC developer with Next.js App Router
  3. Aurora Scharff > Involved in React development community at Meta,

    represented them at their own conference React Conf in May > Spoke at Next.js Conf in San Francisco in October
  4. In this workshop, we will explore React's latest features: Server

    Components, Server Functions, and forms. Discover how to optimize server-side rendering, enhance application interactivity with Server Functions and multiple new React 19 hooks, and create robust forms for efficient data handling and validation. Designed for developers of all levels, this workshop provides practical skills to build scalable, performant web applications.
  5. Workshop plan > Rendering on the web (slides) > App

    Router (slides) > Server components > Client components > Introduction to React 19 (slides) > Server Functions, Actions and forms > React 19 > Exercise (leftover time/take-home)
  6. Static website rendering > Pages are pre-assembled and uploaded to

    a static host > Simple and efficient > Struggles with dynamic data and impractical to maintain
  7. Multi-page applications (MPAs) > HTML and data are dynamically merged

    on the server > Serves the merged result on page load > Page reloads per request
  8. pros √ Optimized for SEO √ Unlimited scalability √ More

    website insights Multi-page applications (MPAs)
  9. pros √ Optimized for SEO √ Unlimited scalability √ More

    website insights cons × Slower perceived performance × Limited interactivity × Hard to maintain Multi-page applications (MPAs)
  10. Server-side rendering (SSR) > Pre-render the initial HTML on the

    server > Fully-formed HTML when opening the page > JavaScript is fetched alongside it
  11. Static site generation (SSG) > Pages are pre-assembled and uploaded

    to a static host > Requires site redeployment whenever data changes > JavaScript is fetched alongside it
  12. Server-side rendering (SSR) > Included feature in JavaScript “meta-frameworks” >

    Build time (SSG) and/or request time (SSR) > Pages generated in the build can be cached
  13. Hydration Hydration is like watering the “dry” HTML with the

    “water” of interactivity and event handlers. - React core team member Dan Abramov https://github.com/reactwg/react-18/discussions/37
  14. The problem 1. You have to fetch everything before you

    can show anything 2. You have to load everything before you can hydrate anything 3. You have to hydrate everything before you can interact with anything
  15. Strategies to improve it > Partial hydration: only attach JS

    to certain components > Progressive hydration: lazy load JS to certain components > Selective hydration: priority load JS to certain components based on user interactivity
  16. Streaming > Split a request into chunks > Send each

    chunk as a stream to the client > Render each chunk as it is streamed in
  17. Next.js App Router An opinionated React meta-framework by Vercel, offering:

    > Server-Side Rendering (SSR), Static Site Generation (SSG), ISR (Incremental Site Regeneration) > Automatic code splitting > File-system based routing and nested layouts > API routes > Caching, Image optimization, SEO, ++ > React Server Components
  18. > Needs server* > Increased complexity > Harder to deploy

    > Learning curve > Opinionated Do I need a meta-framework?
  19. React 19 > Server Components > Actions > Server Functions

    > Form component > New hooks: useActionState(), useOptimistic(), useFormStatus() > use() > … and more https://react.dev/blog/2024/04/25/react-19
  20. Exercise Task 1: Implement const response = confirm('Please confirm you

    want to delete this record.'); if (!response) { return; }
  21. Exercise Task 1: Implement Task 2: Handle loading states for

    mutations with React 19 useFormStatus() https://19.react.dev/reference/react- dom/hooks/useFormStatus const response = confirm('Please confirm you want to delete this record.'); if (!response) { return; }
  22. Exercise Task: Make the favorite-button work Bonus: Make it progressively

    enhanced Bonus: Make it optimistic with React 19 useOptimistic() https://react.dev/reference/react/useOptimistic
  23. Exercise Task: Optimize initial page load time with React 19

    use() and <Suspense/> https://react.dev/reference/react/use
  24. Exercise Task: Make a “chat” or message box for each

    contact. Think about: 1. Loading states 2. Error states 3. Validation 4. Reducing JS on the client 5. Using new React 19 hooks if relevant