New Features in React 19
Tags: react, features
Published: 2024-04-08 13:49:24

In this article, I will share what’s new in React 19 so you can start experimenting with some of the features and learn about what’s changing.

Just keep in mind that as I write this, React 19 is still work in progress. Make sure you follow the official guide on GitHub and the official team on social media to stay updated with the latest developments.

**New Features in React v19 — Overview**
Here’s a quick overview of the new features React 19 will have:

🤖 React compiler: React is working on implementing a new compiler. Currently, Instagram is already leveraging this technology, and it will be released in future versions of React.
🔥 Server components: React has introduced the concept of server components after years of development. You’ll now be able to use this feature with Next.js.
💪 Actions: Actions will also revolutionise how we interact with DOM elements.
📰Document Metadata: Another much-needed improvement is on the horizon, empowering developers to accomplish more with less code.
💼 Assets Loading: This will enable assets to load in the background, which will improve both the application’s load time and the user experience.
⚙️ Web components: This is particularly fascinating: React code will now enable us to incorporate web components. I’m incredibly excited about this development, as it will unlock a myriad of possibilities.
🪝 Enhanced hooks: Exciting new hooks are on the horizon, poised to revolutionise our coding experience.
The constant hunt for code causing re-rendering and the subsequent optimisation efforts have been a recurring task for engineers. But with React 19, this concern will be alleviated. The framework will handle re-rendering automatically, streamlining the development process.

Previously, developers relied on techniques such as useMemo(), useCallback(), memo, and so on to manage re-rendering. But with React 19, such manual interventions will no longer be necessary.

The framework will intelligently identify and memoize code under the hood, resulting in cleaner and more efficient code.

1. 🤖 React Compiler
Currently, React doesn’t automatically re-render on state change. A way to optimise these re-renders is to manually use useMemo(), useCallback(), and memo APIs. As per React's team, this was a "reasonable manual compromise". Their vision was to let React manage these re-renders.

But the React team realized that manual optimisation is cumbersome, and the feedback from the community encouraged them to solve this issue.

And so the React Team has created the “React compiler”. The React compiler will now manage these re-renders. React will decide automatically how and when to change the state and update the UI.

With this, we developers don’t need to do this manually anymore. It also means no need to use useMemo(), useCallback(), and memo.

As a result, React will decide which components to optimise and when, along with what to re-render.

One thing I love about React is that before introducing new breaking changes to the outer world, the React teams use these new features first on their production products . Currently, React Compiler is powering instagram in production.

2. 🔥Server components
If you haven’t heard about server components yet, you’re missing out on one of the most exciting developments in React and Next.js.

Up until now, React components have primarily run on the client side. But React is introducing the groundbreaking concept of running components on the server side.

In React 19, server components will be integrated directly into React, bringing a host of advantages:

SEO: Server-rendered components enhance search engine optimisation by providing more accessible content to web crawlers.
Performance Boost: Server components contribute to faster initial page loads and improved overall performance, particularly for content-heavy applications.
Server-Side Execution: Server components enable executing code on the server, making tasks like API calls seamless and efficient.
These advantages underscore the transformative potential of server components in modern web development.

The below code is in React but will run on the server. You just need to add 'use server' as the first line of the component. This will make the component a "server component". It won't run on the client side and will only run on the server side.

So how can you use a server component?

We can import requestUsername in any React component in the same project. After importing the server component in any React component, we can use "Actions" (we will learn about this soon) to do a specific task.

`'use server';`

`export default async function requestUsername(formData) {
  const username = formData.get('username');
  if (canRequest(username)) {
    // ...
    return 'successful';
  }
  return 'failed';
}`
In the Actions section, you’ll learn more about how to use server components.

Currently Next.js has sever-side component support. You can read more about server components in Next here. With React19, server component support will be available directly in React.

3. 💪Actions
In version 19, another exciting addition will be Actions. This is going to be a game-changer for how we work with forms.

Actions will let you integrate actions with the HTML tag <form/>. In simpler terms, you’ll be able to replace the onSubmit event with Actions. These actions are HTML form attributes.

Before Actions:
In the code snippet below, we’ll utilize the onSubmit React event, which triggers the execution of the search method upon form submission. But it's important to note that currently, the search method runs solely on the client-side. We're limited to using React events for form submission, meaning the search cannot be executed on the server side.

`<form onSubmit={search}>
  <input name="query" />
  <button type="submit">Search</button>
</form>`
After Actions:
With the introduction of server components, Actions can be executed on the server side. In our JSX, with <form/>, we can drop the onSubmit event and use the action attribute. The value of the action attribute will be a method to submit the data either on the client or server side.

You can execute both synchronous and asynchronous operations with actions, streamlining data submission management and state updates. The goal is to make the working with forms and handling data easier.

Let’s look at an example to see how this works:

`"use server"`
`const submitData = async (userData) => {
    const newUser = {
        username: userData.get('username'),
        email: userData.get('email')
    }
    console.log(newUser)
}`
`const Form = () => {
    return <form action={submitData}>
        <div>
            <label>Name</label>
            <input type="text" name='username'/>
        </div>
        <div>
            <label>Name</label>
            <input type="text" name="email" />
        </div>
        <button type='submit'>Submit</button>
    </form>
}`
`export default Form;`
In the above code, submitData is the action in the server component. form is a client side component which is using the submitData as action. submitData will be execute on the server. The communication of the client (form), and server (submitData) components is only possible because of the action attribute.

4. ⚙️ Web components
Ever since, I’ve been captivated by their potential. If you’re not familiar with web components, let me break them down for you:

Web components allow you to create custom components using native HTML, CSS, and JavaScript, seamlessly incorporating them into your web applications as if they were standard HTML tags. Pretty amazing, right?

Currently, integrating web components into React isn’t straightforward. Typically, you either need to convert the web component to a React component or install extra packages and write additional code to make web components work with React. This can be frustrating.

Luckily, React 19 will help you integrate web components into your React code much more easily. If you come across a really useful web component, such as a carousel, you can seamlessly incorporate it into your React projects without the need to convert it into React code.

This streamlines development and allows you to leverage the vast ecosystem of existing web components in your React applications.

But as of now, there are no further details on how the code will look. Still, I am hopeful that it will involve simply importing a web component into a React codebase, similar to module federation. I’m eagerly awaiting further details on this implementation from the React team.

5. 📰 Document Metadata
Elements like “title,” “meta tags,” and “description” are crucial in optimising SEO and ensuring accessibility. In React, where single-page applications are prevalent, managing these elements across different routes can be a bit of a hassle.

Currently, developers often resort to writing custom code, or using packages like react-helmet to handle route changes and update metadata accordingly. This process can be repetitive and error-prone, especially when dealing with SEO-sensitive elements like meta tags.

With React19, we can use the title and meta tags directly in our React components:

`Const HomePage = () => {
  return (
    <>
      <title>Freecodecamp</title>
      <meta name="description" content="Freecode camp blogs" />
      // Page content
    </>
  );
}`
This was not possible before in React. The only way was to use a package like react-helmet.

6. 💼 Asset Loading
In React, you’ll need to carefully manage the loading experience and performance of your applications, particularly with images and other asset files.

Often, the view renders first in the browser, followed by stylesheets, fonts, and images. This can result in a flicker from non-styled (or flash of unstyled content) to a styled view.

To mitigate this issue, developers often resort to adding custom code to detect when these assets are ready, ensuring that the view is displayed only after everything has loaded.

In React 19, images and other files will load in the background as users explore the current page. This improvement should help improve page load times and reduce waiting periods.

7. 🪝 New React Hooks
React Hooks have been one of the most loved features introduced in the library. You have likely used React’s built-in hooks many times, and perhaps you’ve tried making your own custom hooks, too. Hooks are so popular that they’ve become a React programming pattern.

In React 19, the way we use useMemo, forwardRef, useEffect, and useContext will change. This is mainly because a new hook, use, will be introduced.

🎉 New React Hooks
– The new use() hook
– The useFormStatus() hook
– The useFormState() hook
– The useOptimistic() hook

Here’s a quick summary of the exciting changes coming to React v19:
🤖 There will be a new React compiler that will be introduced in a future version of React.
🙌🏽 We’ll now have auto re-rendering, memoization, and optimisation of state and UI.
🔮 There will be some new hooks like use() that will help simplify promises and async code.
⚙️ There will now be server side component support in React.
📝 We’ll have better form handling using actions, useFormStatus(), useStatusForm(), and useOptimistic().
🖼 React will optimise asset loading to enhance performance by using suspense under the hood.
🔗 We’ll have web component integration in React.

If you liked this article, found something you’d like to discuss, or just want to stay connected, come and say hi on [LinkedIn ](https://www.linkedin.com/in/arifalmas/).