-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What’s new in React 19 #6
Comments
Director of Developer Experience React 19 is near. The React Core Team announced a React 19 release candidate (RC) this past April. This major version brings several updates and new patterns, aimed at improving performance, ease of use, and developer experience. Many of these features were introduced as experimental in React 18, but they will be marked as stable in React 19. Here’s a high-level look at what you need to know to be ready. Server ComponentsServer Components are one of the biggest changes to React since its initial release 10 years ago. They act as a foundation for React 19’s new features, improving:
We won’t dive deep into Server Components or rendering strategies in this post. However, to understand the significance of Server Components, let’s take a brief look at how React rendering has evolved. React started with Client-Side Rendering (CSR), which served minimal HTML to the user. index.html <!DOCTYPE html><html> <body> <div id="root"></div> <script src="/static/js/bundle.js"></script> </body></html> The linked script includes everything about your application—React, third-party dependencies, and all your application code. As your application grew, so did your bundle size. The JavaScript is downloaded and parsed, and then React loads the DOM elements into the empty div. While this was happening, all the user sees is a blank page. Even when the initial UI finally shows, the page content is still missing, which is why loading skeletons gained popularity. Data is then fetched and the UI renders a second time, replacing loading skeletons with actual content. React improved with Server-Side Rendering (SSR), which moves the first render to the server. The HTML served to the user wasn’t empty anymore, and it improves how quickly the user saw the initial UI. However, the data still needs to be fetched to display actual content. React frameworks stepped in to further improve the user experience with concepts like Static-Site Generation (SSG), which caches and renders dynamic data during the build, and Incremental Static Regeneration (ISR), which recaches and rerenders dynamic data on-demand. This brings us to React Server Components (RSC). For the first time, native to React, we can fetch data before the UI renders and displays to the user. page.jsx export default async function Page() { const res = await fetch("https://api.example.com/products"); const products = res.json(); return ( <> <h1>Products</h1> {products.map((product) => ( <div key={product.id}> <h2>{product.title}</h2> <p>{product.description}</p> </div> ))} </> );} HTML served to the user is fully populated with actual content on the first render, and there is no need to fetch additional data or render a second time. Server Components are a big step forward for speed and performance, enabling a better developer and user experience. Learn more about React Server Components. Credit to Josh W. Comeau for inspiration on the rendering diagrams. Try Server Components This Next.js App Router template lets you experience Server Components with just a few clicks. New directivesDirectives are not a React 19 feature, but they are related. With the introduction of React Server Components, bundlers need to distinguish where components and functions run. To accomplish this, there are two new directives to be aware of when creating React components:
Learn more about Directives. ActionsReact 19 introduces Actions. These functions replace using event handlers and integrate with React transitions and concurrent features. Actions can be used on both the client and server. For example, you can have a Client Action which replaces previous usage of app.tsx import { useState } from "react";
export default function TodoApp() { const [items, setItems] = useState([ { text: "My first todo" }, ]);
async function formAction(formData) { const newItem = formData.get("item"); // Could make a POST request to the server to save the new item setItems((items) => [...items, { text: newItem }]); }
return ( <> <h1>Todo List</h1> <form action={formAction}> <input type="text" name="item" placeholder="Add todo..." /> <button type="submit">Add</button> </form> <ul> {items.map((item, index) => ( <li key={index}>{item.text}</li> ))} </ul> </> );} Server ActionsGoing further, Server Actions allow Client Components to call async functions executed on the server. This provides additional advantages, like reading the file system or making direct database calls, removing the need for creating bespoke API endpoints for your UI. Actions are defined with the To call a Server Action in a Client Component, create a new file and import it: actions.ts 'use server' export async function create() { // Insert into database} todo-list.tsx "use client";
import { create } from "./actions";
export default function TodoList() { return ( <> <h1>Todo List</h1> <form action={create}> <input type="text" name="item" placeholder="Add todo..." /> <button type="submit">Add</button> </form> </> );} Learn more about Server Actions. New hooksTo complement Actions, React 19 introduces three new hooks to make state, status, and visual feedback easier. These are particularly useful when working with forms, but they can also be used on other elements, like buttons.
|
The original blog info
The text was updated successfully, but these errors were encountered: