Join the discussion
Comments
Loading comments...
Cookie preferences
We use essential cookies for sign-in and preferences. With your permission, we also use basic analytics to improve lessons.
Join the discussion
Loading comments...
We use essential cookies for sign-in and preferences. With your permission, we also use basic analytics to improve lessons.
React 18 time slicing aur priority lanes se UI ko responsive kaise rakhta hai.
Concurrent rendering React 18 ki woh capability hai jo ek saath UI ke multiple versions tayaar kar sakti hai aur zyada urgent updates handle karne ke liye lambe-chalte renders ko interrupt kar sakti hai. Ek single synchronous operation ki jagah jo main thread block kare, React ab render work ko tree ke beech mein pause kar sakta hai, browser ko yield kar sakta hai, urgent updates handle kar sakta hai, phir paused work resume ya discard kar sakta hai.
React ka Fiber reconciler har component ko kaam ki ek chhoti unit (ek fiber) ki tarah represent karta hai. Render phase ek baar mein ek fiber process karta hai, har unit ke baad check karta hai ki koi higher-priority kaam aaya hai ya nahi. Agar aaya hai, toh woh pause karke baad mein aa sakta hai.
React ka scheduler render work ko chhote chunks mein todta hai. Har chunk ke baad, woh browser ko control wapas deta hai â jisse input events, animations aur paints ho sakein. Isse lambe renders ke dauran UI freeze nahi hota.
React 18 updates ko priority ke hisaab se classify karne ke liye ek 'lanes' model use karta hai: SyncLane (user input, turant hona chahiye), InputContinuousLane (ongoing gestures), DefaultLane (normal state updates), TransitionLane (non-urgent UI transitions).
Agar ek high-priority update aata hai jab React low-priority render par kaam kar raha hota hai, toh React low-priority kaam pause karta hai, high-priority update completely process karta hai (commit bhi), phir low-priority kaam resume ya restart karta hai.
Kyunki render phase pure hai (koi side effects nahi), React safely partially-completed renders discard karke unhe restart kar sakta hai bina application state corrupt kiye. Side effects sirf commit phase mein hote hain, jo kabhi interrupt nahi hota.
Render work ko ~5ms chunks mein todna aur chunks ke beech browser ko yield karna. Expensive renders ke dauran UI ko responsive rakhta hai.
Ek bitmask system jo updates ko SyncLane (sabse urgent) se OffscreenLane (sabse kam urgent) tak classify karta hai. Scheduling order determine karta hai.
React ka internal task scheduler. Render tasks ki ek priority queue manage karta hai aur priority aur deadlines ke basis par decide karta hai ki aage kya karna hai.
Ek state update ko non-urgent transition mark karne ka API. React ko batata hai ki yeh kaam urgent updates ke liye interrupt kiya ja sakta hai.
Hook jo startTransition ke saath ek isPending boolean bhi deta hai jo batata hai ki koi transition in progress hai ya nahi.
1import { useState, useTransition } from 'react';23function SearchPage() {4 const [query, setQuery] = useState('');5 const [results, setResults] = useState([]);6 const [isPending, startTransition] = useTransition();78 function handleInput(e) {9 // Urgent: update the input immediately10 setQuery(e.target.value);1112 // Non-urgent: filtering 10,000 results can wait13 startTransition(() => {14 setResults(filterItems(e.target.value));15 });16 }1718 return (19 <>20 <input value={query} onChange={handleInput} />21 {isPending && <Spinner />}22 <ResultsList results={results} />23 </>24 );25}26// Typing feels instant (urgent update)27// Results update when React has time (transition)
React 18 se pehle, koi bhi state update jo slow render trigger karta tha, poori UI rendering complete hone tak freeze kar deta tha. Concurrent rendering ka matlab hai ki input type karna, scroll karna, aur buttons click karna responsive rehte hain chahe React background mein ek complex update compute kar raha ho. Yeh Suspense, streaming SSR, aur React Server Components ki neenv hai.
A product catalog with 50,000 items needs instant search. Using debounce (300ms delay) feels sluggish. Without debounce, every keystroke triggers a heavy filter that blocks the main thread and makes typing lag.
Traditional approach: debounce â wait 300ms â filter â re-render. The user types 'laptop' and sees nothing for 300ms after each keystroke. Or without debounce: type 'l' â 50ms filter blocks thread â 'a' keystroke is delayed â laggy typing experience.
Use `startTransition` to mark the filter as non-urgent. React processes the input update immediately (typing stays responsive), then renders the filtered results when the main thread is free. If the user types another character before the filter completes, React ABANDONS the in-progress render and starts the new filter â no wasted work.
Takeaway: Concurrent rendering replaces debouncing for CPU-heavy UI updates. It provides a better UX (no artificial delay) with better performance (abandoned renders waste no DOM work). Use useTransition for filtering, sorting, and computing derived state.
Clicking a navigation link to a data-heavy dashboard page freezes the UI for 500ms while the new page renders 200+ chart components. The old page disappears, a blank screen shows, then the new page appears â jarring.
Without concurrent rendering, React must complete the entire render synchronously. Clicking 'Dashboard' unmounts the current page and starts rendering 200 charts. The main thread is blocked for 500ms â no animations, no response to user input, just a white flash.
Wrap the navigation in `startTransition(() => navigate('/dashboard'))`. React keeps the old page visible and interactive while rendering the dashboard in the background. If the user clicks 'Back' during this time, React abandons the dashboard render. The transition completes smoothly with no blank screen.
Takeaway: Concurrent rendering enables 'optimistic UI' for navigation â the old page stays visible until the new one is fully ready. This eliminates white-flash navigation transitions and lets users cancel slow navigations by clicking elsewhere. Next.js App Router uses this pattern by default.