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

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

Aurora Scharff
September 16, 2024
13

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

In this workshop, we will explore React's latest features: Server Components, Server Actions, 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 Actions, and Forms in the

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

    Oslo > Full-stack, web-focused: active RSC developer
  3. HVEM ER JEG? Aurora Scharff > Involved in React development

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

    Components, Server Actions, 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.
  5. Workshop plan > Rendering on the web (slides) > App

    Router (slides) > Server components > Client components > Introduction to React 19 (slides) > Server functions 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. “Progressive enhancement is a strategy that puts emphasis on web

    content first, allowing everyone to access the basic functionality whilst users with additional browser features or faster Internet access receive an enhanced version instead.” https://en.wikipedia.org/wiki/Progressive_enhancement
  20. 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
  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) {
  22. Exercise Task 1: Make the favorite-button work Task 2: Make

    it progressively enhanced Task 3: Make it optimistic with React 19 useOptimistic() https://react.dev/reference/react/useOptimistic
  23. 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