React view transitions, automatic memoization, and Effect Events

Post on X
Copy URL
Share

React is one of the most widely used libraries in web development. The first article in this series looked at how React can keep an interface responsive while data loads or updates. Previous article

This article looks at the developer’s side of that work. You’ll animate a list as it changes order, let React Compiler handle memoization, and build an autosave timer that keeps running as you type. Then you’ll use Chrome DevTools to see what React does during an update.

Animate a list as it changes order

When you switch screens or reorder a list, an animation can help people follow an element from its old position to its new one. The browser’s View Transitions API, available in Chrome since version 111 in March 2023, made these animations easier to build. Introduction to the View Transition API for seamless page transitions

React 19.3, released in September 2026, made the <ViewTransition> component stable. It uses the browser’s View Transitions API under the hood.

Wrap each item in <ViewTransition>

Use <ViewTransition> to animate elements as they appear, disappear, or move. In the demo, switch between Name and Branch and watch each person move to a new position in the list.

The list needs three things to animate when the sort order changes.

  • Store the current sort order in state (sortKey).
  • Wrap the update to sortKey in startTransition.
  • Wrap each list item in <ViewTransition>, using the person’s ID as its key.
import { startTransition, useState, ViewTransition } from "react";
import { ToggleGroup } from "../../components/ToggleGroup.jsx";
import { users } from "./users.js";

export const ViewTransitionPage = () => {
  const [sortKey, setSortKey] = useState("name");
  const sortedUsers = [...users].sort((a, b) =>
    a[sortKey].localeCompare(b[sortKey], "en"),
  );

  const changeSort = (nextSortKey) => {
    startTransition(() => setSortKey(nextSortKey));
  };

  return (
    <div className="view-transition">
      <ToggleGroup
        label="sort"
        options={[
          { label: "Name", value: "name" },
          { label: "Branch", value: "branch" },
        ]}
        value={sortKey}
        onChange={changeSort}
      />
      <UserList users={sortedUsers} />
    </div>
  );
};

const UserList = ({ users }) => (
  <ul>
    {users.map((user) => (
      <ViewTransition key={user.id}>
        <li>{user.name}</li>
      </ViewTransition>
    ))}
  </ul>
);

The key stays with the person as the list changes order, so React can animate the same item between the two layouts.

Why does the sort handler call startTransition?

In this example, calling setSortKey on its own reorders the list immediately. Wrapping that update in startTransition marks it as a React Transition, which lets <ViewTransition> animate the change.

Both startTransition and the useTransition Hook can mark a state update as lower priority. React can then handle urgent work, such as user input, first. You can also use <ViewTransition> with <Suspense> or useDeferredValue, both covered earlier in this series. Previous article

Let React handle memoization

React developers have long used useMemo, useCallback, and memo to avoid repeating calculations or rendering a component again with the same props. This is called memoization. It often takes coordination between a parent and its children. Getting started with React Hooks

In this example, useMemo caches the result of an expensive calculation. The button can skip a render because memo checks whether its props have changed and useCallback keeps its onClick prop stable.

import { memo, useCallback, useMemo } from "react";

const SomeComponent = ({ id, text, onClickButton }) => {
  // Recalculate only when the text changes.
  const formatted = useMemo(() => expensiveFunc(text), [text]);
  // Reuse the handler while its dependencies stay the same.
  const handleClick = useCallback(() => onClickButton(id), [onClickButton, id]);

  return (
    <>
      <p>{formatted}</p>
      <SomeButton onClick={handleClick} />
    </>
  );
};

const SomeButton = memo(({ onClick }) => (
  <button type="button" onClick={onClick}>
    Some Button
  </button>
));

Write the same components with React Compiler

React Compiler optimizes React code at build time. Once it’s enabled, you can usually write the calculation and the click handler directly in the component. Here’s the same example with the compiler handling memoization. React Compiler

export const SomeComponent = ({ id, text, onClickButton }) => (
  <>
    <p>{expensiveFunc(text)}</p>
    <SomeButton onClick={() => onClickButton(id)} />
  </>
);

const SomeButton = ({ onClick }) => (
  <button type="button" onClick={onClick}>
    Some Button
  </button>
);

The setup depends on your build tools. See the installation instructions for the plugin or configuration your project needs for Vite, Next.js, or React Router. Installation

Keep an Effect from restarting on every keystroke

An Effect runs again when one of its dependencies changes. Every reactive value it reads, including state, belongs in its dependency array. Sometimes an Effect needs to read the latest value, but restarting the work on every change causes a problem.

Try typing continuously in Only useEffect on the left. This note editor is supposed to save and display the current draft every five seconds. Each keystroke resets the countdown. If you keep typing, the draft doesn’t get saved until you pause for five seconds.

The autosave callback captures content from the current render, so content is an Effect dependency. Every keystroke repeats the same five steps.

  1. You type in the text area.
  2. The input handler updates content.
  3. React cleans up the previous Effect, stopping its timers, and runs the Effect again.
  4. The Effect schedules the next save for five seconds from now.
  5. The countdown starts again at five.
const [content, setContent] = useState("");
const [saved, setSaved] = useState("");
const [secondsRemaining, setSecondsRemaining] = useState(5);

useEffect(() => {
  const stopAutoSave = autoSave(() => setSaved(content));
  const stopCountdown = countdown(setSecondsRemaining);

  return () => {
    stopAutoSave();
    stopCountdown();
  };
}, [content]);

Read the latest draft with useEffectEvent

Starting a timer and deciding what to save are two different jobs. The timer should keep its five-second interval. When it fires, the save callback should read the latest content. Moving that read into an Effect Event keeps typing from restarting the timer.

const [content, setContent] = useState("");
const [saved, setSaved] = useState("");
const [secondsRemaining, setSecondsRemaining] = useState(5);

const saveDraft = useEffectEvent(() => {
  setSaved(content);
});

useEffect(() => {
  const stopAutoSave = autoSave(() => saveDraft());
  const stopCountdown = countdown(setSecondsRemaining);

  return () => {
    stopAutoSave();
    stopCountdown();
  };
}, []);

The Effect sets up the timers, and saveDraft reads the current text each time the autosave timer fires. Try typing in With useEffectEvent on the right. The saved draft updates every five seconds, even while you type.

Use Effect Events only for work that belongs in an Effect

Overusing Effects can trigger unnecessary work or cause side effects you didn’t expect. Using useEffectEvent just to keep an Effect from running again also makes it harder to see which changes should trigger the work.

First ask whether you need an Effect. If you do, an Effect Event can read the latest value without making it a dependency. You Might Not Need an Effect

See when React schedules and renders work

Once an interaction involves state updates, re-renders, and animations, it can be hard to tell what happened first. React 19.2, released in October 2025, added React Performance tracks to the Performance panel in Chrome DevTools.

Read an update on the timeline

The tracks show when React schedules work, which components render, and how long each render takes. They’re enabled by default in a development build, such as one started with pnpm dev.

Record an interaction

  1. Right-click the page and choose Inspect to open Chrome DevTools.
  2. Select the Performance panel.
  3. Click Record in the upper-left corner.
  4. Interact with the page, then stop the recording.

React Scheduler and Components tracks in the Chrome DevTools Performance panel

Try recording the price calculator from the first article. One version uses useTransition to give the total-price update a lower priority. Standard production builds disable the tracks, so this demo uses a profiling build to make them available. Using profiling builds

Change the quantity in each version and compare the recordings. The <Transition> component uses useTransition, and its recording includes entries labeled Suspended and Action. The <NoTransition> component shows the same interaction without the Hook.

Scheduler track with Suspended and Action entries during a Transition

Scheduler track

This is where the Suspended and Action entries appear. The Scheduler track groups React’s internal work by priority.

  • Blocking includes synchronous updates, such as those triggered by user input.
  • Transition shows work started as a Transition, including updates wrapped in startTransition in the animation demo or scheduled through useTransition.
  • Suspense includes work such as showing a <Suspense> fallback.
  • Idle shows work React handles at its lowest priority.

Components track

The Components track shows the order in which components render and how long each takes. In the recording below, you can see components from the demo, including <NoTransition> and <ToggleGroup>.

Components track showing render times for NoTransition and ToggleGroup

Column – catching React mistakes with a linter

Static analysis can catch patterns that cause unexpected behavior or slow down rendering. Linters such as ESLint and Oxlint can flag problems before you run the app.

ESLint lets you add rules through plugins. For a React project, eslint-plugin-react-hooks can catch the following mistakes.

  • Leaving a dependency out of an Effect’s dependency array
  • Updating state synchronously in an Effect when the update is unnecessary
  • Unconditionally updating state during render
  • Calling useMemo with a callback that never returns a value

If you’re working with React, enable these checks as part of your development workflow.

Conclusion

Reordering a list once took custom animation code. Now a keyed <ViewTransition> can animate each item, and React Compiler can handle much of the memoization you used to write by hand. When an update still behaves unexpectedly, the Performance tracks let you see which components actually rendered. Try recording an interaction in your own app and following it from the Scheduler track to the Components track.

Find ICS MEDIA articles more easily on Google

Add ICS MEDIA as a preferred source to see our articles more often in Top Stories and AI Search.

Add as a preferred source on Google
Share on social media
Your shares help us keep the site running.
Post on X
Copy URL
Share
KITAGAWA Kyoko

Started in apparel and office work, then transitioned into engineering. Joined ICS after full-stack experience in both backend and frontend development. Special skill: English.

Articles by this staff