Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
useTransition is a Hook that lets you mark certain state updates as non-urgent transitions. This keeps your UI responsive by allowing React to interrupt less important updates and show loading states!
Blocks UI until complete
UI stays responsive
With useTransition, React can:
First, import useTransition and destructure the isPending state and startTransition function.
Two Values Returned
isPending tracks loading state, startTransition wraps non-urgent updates.
Wrap expensive state updates in startTransition to mark them as non-urgent.
Non-Blocking Updates
State updates inside startTransition won't block user interactions!
Use the isPending state to show loading indicators during transitions.
Visual Feedback
isPending is true during transitions, perfect for loading spinners!
Keep urgent updates (like input values) outside of transitions for immediate feedback.
Best Practice
Input values update immediately, search results update as transitions!
Experience smooth UI interactions with useTransition
function ResponsiveSearchExample() {
const [inputValue, setInputValue] = React.useState('');
const [searchResults, setSearchResults] = React.useState([]);
const [isPending, startTransition] = React.useTransition();
// Simulate expensive search operation
const performSearch = (query) => {
const allItems = [
'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry',
'Fig', 'Grape', 'Honeydew', 'Kiwi', 'Lemon',
'Mango', 'Orange', 'Papaya', 'Quince', 'Raspberry',
'Strawberry', 'Tangerine', 'Ugli fruit', 'Vanilla', 'Watermelon'
];
// Simulate slow search
const start = Date.now();
while (Date.now() - start < 500) {} // 500ms delay
return allItems.filter(item =>
item.toLowerCase().includes(query.toLowerCase())
);
};
const handleChange = (e) => {
const value = e.target.value;
// Urgent update - immediate feedback
setInputValue(value);
// Non-urgent update - wrapped in transition
startTransition(() => {
setSearchResults(performSearch(value));
});
};
return (
<div className="container">
<h2>Responsive Search with useTransition</h2>
<p>Try typing quickly - the input stays responsive while search results load!</p>
<div className="search-container">
<input
type="text"
value={inputValue}
onChange={handleChange}
placeholder="Search fruits..."
className="search-input"
/>
{isPending && (
<div className="loading-indicator">
<Loader2 className="animate-spin" />
Searching...
</div>
)}
</div>
<div className="results-container">
{isPending ? (
<div className="loading-placeholder">
Loading results...
</div>
) : (
<div className="results-list">
{searchResults.map((item, index) => (
<div key={index} className="result-item">
{item}
</div>
))}
</div>
)}
</div>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ResponsiveSearchExample />);Loading preview...
Large datasets where filtering takes time but input should stay responsive
Charts and graphs that need time to render but shouldn't block UI
Page transitions and route changes with loading states
Input values, button states, or anything needing instant feedback
Simple state changes that don't impact performance
Use useEffect with loading states for API calls instead
State updates in transitions don't block user interactions or UI rendering.
isPending provides built-in loading state for better user experience.
Transitions can be interrupted by more urgent updates, keeping UI responsive.
Works with React's concurrent features for optimal performance.