Animation and Transitions in React
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Animate with CSS transitions and the View Transitions API.
- Respect prefers-reduced-motion.
- Explain why animation libraries keep their own state.
Overview
#Prefer CSS for simple transitions
Most UI animation is a class toggle. Adding or removing a class that changes transform or opacity lets the browser animate on the compositor, off the main thread. React only needs to change the class; the browser does the rest.
<div className={open ? 'panel panel-open' : 'panel'}>...</div>
View Transitions
The browser View Transitions API, document.startViewTransition, animates between the old and new DOM. It is a platform API, not a React hook: startViewTransition is not exported by React, and using it directly with React is fiddly because the DOM update inside its callback must be synchronous (usually wrapped in flushSync).
React has an experimental integration, <ViewTransition> (canary builds of the React 19 line, not stable core), that calls document.startViewTransition for you. Only updates wrapped in startTransition, useDeferredValue or a Suspense boundary activate it; a plain setState does not. Treat it as an opt-in, environment-dependent feature and always check browser support and prefers-reduced-motion.
Enter and exit animations
CSS transitions handle enter and exit only if the element stays mounted long enough to animate out. To animate exit, keep the element mounted until the transition ends, then unmount. Libraries such as Framer Motion track this lifecycle for you.
// mounted stays true during the exit transition; onExited clears it
{mounted && <Panel open={visible} onExited={() => setMounted(false)} />}
Respect reduced motion
Honor prefers-reduced-motion. Users who request reduced motion may experience discomfort from large movement. Provide a reduced or no-motion variant.
@media (prefers-reduced-motion: reduce) {
.panel { transition: none; }
}
Pitfall: animating layout properties
Animating width, height, top or left triggers layout on every frame and janks. Animate transform and opacity, which the compositor can handle. Measure with a layout effect only when you must.
Pitfall: React state per frame
Driving an animation by setting React state on every frame re-renders the tree dozens of times a second. Let CSS or the Web Animations API run the animation and keep React out of the frame loop.
Library state is separate
Animation libraries maintain their own animation state outside React and sync it to the DOM. That is why they can animate smoothly without re-rendering. Integrate them through refs and imperative handles rather than state.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A drawer's exit animation never plays. Why?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsx{open && <Drawer className={open ? 'open' : 'closed'} />}Exercise 2Implement
Animate a panel open and closed with a CSS transition that respects reduced motion.
starter.tsxexport function Panel({ open, children }: { open: boolean; children: React.ReactNode }) { return <div>{children}</div>; } /* panel.css */Reveal the reference answer
reference.tsxexport function Panel({ open, children }: { open: boolean; children: React.ReactNode }) { return <div className={open ? 'panel panel-open' : 'panel'}>{children}</div>; } /* panel.css */ .panel { transform: translateX(-100%); transition: transform 200ms ease; } .panel-open { transform: translateX(0); } @media (prefers-reduced-motion: reduce) { .panel { transition: none; } }Authored expected output: The panel slides in and out with a compositor-friendly transform, and does not animate when reduced motion is requested.
The class toggle drives a transform transition, which the compositor can animate without layout. The media query disables the transition for users who prefer reduced motion.
Exercise 3Debug
This progress animation stutters. Explain and fix.
starter.tsxuseEffect(() => { const id = setInterval(() => setProgress((p) => p + 1), 16); return () => clearInterval(id); }, []);Reveal the reference answer
reference.tsx// Set progress only when the target changes; CSS animates between values. <div className="bar" style={{ transform: `scaleX(${progress / 100})` }} /> /* bar.css */ .bar { transform-origin: left; transition: transform 1s linear; }Authored expected output: The bar animates smoothly without per-frame React renders.
Setting state every 16ms re-renders the component about 60 times a second, which competes with the browser's work. Set progress once when the target changes and let a CSS transform transition interpolate on the compositor. Animating transform, not width, also avoids layout on every frame.
Exercise 4Interview
Explain how you would add a smooth enter and exit animation to a list item in React.
starter.tsx// enter, exit, reduced motion, layoutReveal the reference answer
reference.tsxFor enter, render the item with a class that transitions opacity and transform from a hidden state to the visible state. For exit, keep the item mounted while a class drives it to the hidden state, then unmount when the transition ends, using a transitionend listener or a library that tracks the lifecycle. Animate only transform and opacity to stay on the compositor and avoid layout. Honor prefers-reduced-motion by skipping the transition. Keep React out of the frame loop so the animation does not cause re-renders.Authored expected output: A plan covering enter, exit with delayed unmount, compositor-friendly properties and reduced motion.
The answer should address the exit-unmount problem and the performance of the animated properties, plus accessibility.
Common misconceptions
#- Animating width and height is fine. Layout properties trigger layout every frame; animate transform and opacity.
- React state should drive each animation frame. Keep React out of the frame loop; let CSS or the Web Animations API run it.
- prefers-reduced-motion can be ignored. Respect it with a reduced or no-motion variant.
- CSS can animate exit automatically. An exiting element must stay mounted until the transition ends.
Follow-up practice
#- react-component-architecture
- react-accessibility
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.