What’s New in React 19: A Game of Thrones Saga

Jorge Madson - Aug 5 - - Dev Community

What’s New in React 19: A Game of Thrones Saga

Winter is coming, and so is React 19, with a flurry of new features that would make even the most stoic Starks crack a smile. Gather 'round the fire, dear developers, as we recount the tales of transitions, hooks, and the dreaded Server Components, all through the lens of Westeros.

The Battle of Actions

In the land of React, handling data mutations and updating state was once as cumbersome as defending the Wall from a horde of White Walkers. With React 18, developers had to juggle pending states, errors, and sequential requests like Jon Snow balancing his duty and love life.

// Before Actions
function UpdateName({}) {
  const [name, setName] = useState("");
  const [error, setError] = useState(null);
  const [isPending, setIsPending] = useState(false);

  const handleSubmit = async () => {
    setIsPending(true);
    const error = await updateName(name);
    setIsPending(false);
    if (error) {
      setError(error);
      return;
    } 
    redirect("/path");
  };

  return (
    <div>
      <input value={name} onChange={(event) => setName(event.target.value)} />
      <button onClick={handleSubmit} disabled={isPending}>
        Update
      </button>
      {error && <p>{error}</p>}
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

But lo, in React 19, the mighty Actions have arrived, wielding the power of useTransition to handle these tasks automatically. It’s as if Daenerys finally learned to say "Dracarys" at the right time.

// Using pending state from Actions
function UpdateName({}) {
  const [name, setName] = useState("");
  const [error, setError] = useState(null);
  const [isPending, startTransition] = useTransition();

  const handleSubmit = () => {
    startTransition(async () => {
      const error = await updateName(name);
      if (error) {
        setError(error);
        return;
      } 
      redirect("/path");
    })
  };

  return (
    <div>
      <input value={name} onChange={(event) => setName(event.target.value)} />
      <button onClick={handleSubmit} disabled={isPending}>
        Update
      </button>
      {error && <p>{error}</p>}
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

The Hook of Optimism

Remember when Tyrion tried to convince everyone that he had a plan? In React 19, the useOptimistic hook is your optimistic Tyrion. It helps you manage optimistic updates, ensuring your UI remains responsive while data is being fetched. It’s like knowing the dragons are on their way, even if you can’t see them yet.

function ChangeName({currentName, onUpdateName}) {
  const [optimisticName, setOptimisticName] = useOptimistic(currentName);

  const submitAction = async formData => {
    const newName = formData.get("name");
    setOptimisticName(newName);
    const updatedName = await updateName(newName);
    onUpdateName(updatedName);
  };

  return (
    <form action={submitAction}>
      <p>Your name is: {optimisticName}</p>
      <p>
        <label>Change Name:</label>
        <input
          type="text"
          name="name"
          disabled={currentName !== optimisticName}
        />
      </p>
    </form>
  );
}

Enter fullscreen mode Exit fullscreen mode

The Scroll of Server Components

Ah, the Server Components—whispered about in hushed tones across the Seven Kingdoms. These components allow rendering ahead of time, much like Bran Stark seeing the future. They can run once at build time or for each request using a web server.

import {use} from 'react';

function Comments({commentsPromise}) {
  const comments = use(commentsPromise);
  return comments.map(comment => <p key={comment.id}>{comment}</p>);
}

function Page({commentsPromise}) {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <Comments commentsPromise={commentsPromise} />
    </Suspense>
  );
}
Enter fullscreen mode Exit fullscreen mode

These Server Components are as stable as the Iron Throne, ready to support your most complex applications without breaking between major versions.

Conclusion

React 19 brings a plethora of new features that would make any house in Westeros proud. From the powerful Actions to the foresight of Server Components, it’s a release fit for a king—or at least a Hand of the King.

So, as you prepare for the next season of your coding adventures, remember the words of House React: "Winter is coming, but so is React 19."

Brace yourselves, developers, and may your code be bug-free and your builds swift.

. . . .
Terabox Video Player