React 19.3 made ViewTransition a stable component. It can give a visual cue when part of a page changes. In this lesson, you will switch between two student cards and see how startTransition marks the update as a transition. This feature is for React DOM; the animation also depends on browser support for the View Transition API.
Button click → startTransition → React updates card → Browser animates if supported
Before you start
Know basic JSX, components and useState. Install a supported Node.js LTS version. Create a Vite React project with npm create vite@latest student-cards -- --template react, enter its folder, then install the stable React 19.3 line with npm install react@^19.3.0 react-dom@^19.3.0. Run npm list react react-dom and make sure both packages are 19.3 or later. The example below replaces src/App.jsx.
Student card component
The array is a tiny data source. The state variable index chooses one student. The button updates that state inside startTransition. ViewTransition wraps the part of the page that changes; the key identifies which card is shown.
import { startTransition, useState, ViewTransition } from 'react';
const students = [
{ name: 'Asha', subject: 'React', progress: 75 },
{ name: 'Ravi', subject: 'Python', progress: 45 },
];
export default function App() {
const [index, setIndex] = useState(0);
const student = students[index];
function showNext() {
startTransition(() => {
setIndex((current) => (current + 1) % students.length);
});
}
return (
<main>
<h1>Student progress</h1>
<p>Press the button to see the next student.</p>
<ViewTransition>
<section className="card" key={student.name} aria-live="polite">
<h2>{student.name}</h2>
<p>Subject: {student.subject}</p>
<p>Completed: {student.progress}%</p>
</section>
</ViewTransition>
<button type="button" onClick={showNext}>Next student</button>
</main>
);
}
Add a small style sheet
Save this as src/style.css, then import it from src/main.jsx. Keep the generated createRoot call. The last rule respects a user's reduced-motion setting.
body { font-family: system-ui, sans-serif; margin: 0; background: #f4f7fc; color: #17233b; }
main { max-width: 600px; margin: 40px auto; padding: 20px; }
.card { background: white; border: 1px solid #d9e4f5; border-radius: 14px; padding: 22px; box-shadow: 0 8px 24px #152b4715; }
button { margin-top: 20px; border: 0; border-radius: 8px; background: #265ef6; color: white; font: inherit; padding: 10px 16px; cursor: pointer; }
button:focus-visible { outline: 3px solid #d28c00; outline-offset: 3px; }
@media (prefers-reduced-motion: reduce) { ::view-transition-old(*), ::view-transition-new(*) { animation-duration: 0.01ms !important; } }
If your Vite starter already imports another CSS file, remove or replace that import so the example's styles are clear. Start the app with npm run dev. Press Next student: Asha and Ravi should alternate. On a browser with View Transition support, the card change uses React's default cross-fade. Without that browser feature, the content still changes.
Why use startTransition?
React's View Transition animation responds to updates marked as transitions. An ordinary urgent state update is shown immediately and does not start this animation. Use transitions for non-urgent visual changes; keep typing and other urgent interactions responsive.
Try it yourself
- Add a third student. Predict the order of cards.
- Change a progress value and explain which part of the card updates.
- Turn on reduced motion in your operating system and check that the page remains usable.
Continue with the React.js learning page. Read the React 19.3 release post and ViewTransition reference for animation details and browser behavior.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.