Lesson 2 of 7
useTransition - Non-Urgent Updates
useTransition marks state updates as non-urgent transitions. React deprioritizes transition renders, allowing urgent updates (user typing) to interrupt them. isPending lets you show a subtle loading indicator without a full loading spinner replacing the UI.
useTransition - Real-World Usage
import { useState, useTransition, useDeferredValue, Suspense } from 'react';
// ---- useTransition - mark expensive state updates as non-urgent ----
function SearchPage() {
const [query, setQuery] = useState('');
const [searchResults, setSearchResults] = useState<Result[]>([]);
const [isPending, startTransition] = useTransition();
function handleSearch(value: string) {
setQuery(value); // urgent - input updates immediately
startTransition(() => {
// non-urgent - React may delay this if user keeps typing
const results = performExpensiveSearch(value); // CPU-intensive
setSearchResults(results);
});
}
return (
<>
<input
value={query}
onChange={e => handleSearch(e.target.value)}
// Input is NEVER blocked by the slow results render
/>
{/* Subtle dimming during pending transition */}
<div style={{ opacity: isPending ? 0.5 : 1, transition: 'opacity 0.2s' }}>
<ResultList results={searchResults} />
</div>
{/* No flash - results dim slightly instead of showing spinner */}
</>
);
}
// ---- useTransition for navigation ----
function App() {
const [page, setPage] = useState('home');
const [isPending, startTransition] = useTransition();
function navigate(newPage: string) {
startTransition(() => setPage(newPage));
// The current page stays visible during transition
// New page loads with Suspense (streaming)
}
return (
<>
<nav>
<button onClick={() => navigate('home')} disabled={isPending}>Home</button>
<button onClick={() => navigate('dashboard')} disabled={isPending}>Dashboard</button>
{isPending && <Spinner small />}
</nav>
<Suspense fallback={<PageSkeleton />}>
{page === 'home' ? <HomePage /> : <DashboardPage />}
</Suspense>
</>
);
}useDeferredValue - Deferred Rendering
// useDeferredValue - defer updating a part of the UI
// Similar to useTransition but for values you don't control
function SearchResults({ query }: { query: string }) {
// Defers updating the displayed results while user keeps typing
const deferredQuery = useDeferredValue(query);
// Memoize - only re-renders when deferredQuery changes
const results = useMemo(
() => performExpensiveFilter(bigDataSet, deferredQuery),
[deferredQuery]
);
// Subtle indicator that we're showing stale results
const isStale = query !== deferredQuery;
return (
<ul style={{ opacity: isStale ? 0.7 : 1 }}>
{results.map(r => <li key={r.id}>{r.name}</li>)}
</ul>
);
}
// useTransition vs useDeferredValue:
// useTransition: you CONTROL the state update (wrap with startTransition)
// useDeferredValue: you DON'T control the update (e.g., prop from parent)Tip
Tip
Practice useTransition NonUrgent Updates in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for useTransition - Non-Urgent Updates
Practice Task
Note
Practice Task - (1) Write a working example of useTransition NonUrgent Updates from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with useTransition NonUrgent Updates is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Finished this lesson?