The useTransition hook is a React 18 Concurrent feature that allows state updates to be marked as low-priority transitions.
By default, all state updates in React are high-priority and synchronous. useTransition tells React that a specific state update can be interrupted by higher-priority user interactions (like typing in an input or clicking a tab).
const [isPending, startTransition] = useTransition();
isPending: Boolean flag indicating whether a low-priority transition is currently processing.startTransition(callback): Function wrapper used to mark state updates as non-blocking transitions.import React, { useState, useTransition } from 'react';
function AboutTab() {
return <p>Welcome to our company overview page.</p>;
}
function PostsTab() {
// Simulate heavy component rendering
const posts = Array.from({ length: 5000 }, (_, i) => `Post content item #${i + 1}`);
return (
<ul>
{posts.map((p, idx) => (
<li key={idx}>{p}</li>
))}
</ul>
);
}
export default function TabContainer() {
const [activeTab, setActiveTab] = useState('about');
const [isPending, startTransition] = useTransition();
const handleTabChange = (tabName) => {
// Wrap heavy state update inside startTransition
startTransition(() => {
setActiveTab(tabName);
});
};
return (
<div className="tabs-wrapper">
<nav className="tab-buttons">
<button onClick={() => handleTabChange('about')}>About</button>
<button onClick={() => handleTabChange('posts')}>
Posts {isPending && '(Loading...)'}
</button>
</nav>
<main style={{ opacity: isPending ? 0.6 : 1 }}>
{activeTab === 'about' && <AboutTab />}
{activeTab === 'posts' && <PostsTab />}
</main>
</div>
);
}
onChange handlers must remain high-priority to ensure smooth typing.startTransition: Pass state updater calls (setTab(...)) directly inside the startTransition callback.Explain how useTransition improves UI responsiveness over synchronous state updates during heavy component rendering.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With