๐ Behind The Scenes - React Kaise Render Karta Hai?
๐ Rendering Process (Step by Step):
Trigger: Initial render ya state/props change
Render Phase: React components ko call karta hai aur Virtual DOM tree banata hai
Reconciliation: Purane aur naye Virtual DOM ko compare karta hai
Commit Phase: Changes ko Real DOM mein apply karta hai
Browser Paint: Browser screen ko update karta hai
// 1. Component function call hota hai
function Counter() {
const [count, setCount] = useState(0);
// 2. JSX return hota hai (React.createElement calls)
return <div>{count}</div>;
}
// 3. React internally ye banata hai:
React.createElement('div', null, count);
// 4. Virtual DOM node banta hai:
{ type: 'div', props: { children: 0 } }
// 5. Reconciliation ke baad Real DOM update hota hai
๐ผ Interview Questions
Click on questions to see answers!
Q1. React kya hai aur iske main features kya hain?Easy
Answer: React ek JavaScript library hai jo user interfaces banane ke liye use hoti hai. Main features:
Component-Based: UI ko reusable pieces mein divide karte hain
Virtual DOM: Efficient updates ke liye
Declarative: UI ki final state describe karte hain, React baaki sambhal leta hai
JSX: HTML-like syntax JavaScript mein
One-way Data Flow: Data parent se child ki taraf flow hota hai
Q2. Virtual DOM kya hai aur ye Real DOM se fast kyun hai?Easy
Answer: Virtual DOM React ka lightweight JavaScript representation hai Real DOM ka. Ye fast hai kyunki:
Ye memory mein rehta hai, browser paint nahi karta
React diffing algorithm se sirf actual changes find karta hai
Batch updates - multiple changes ko ek saath apply karta hai
Real DOM manipulation expensive hai (reflow, repaint), Virtual DOM mein ye nahi hota
Efficient reconciliation process se minimum DOM updates hote hain
Q3. SPA aur MPA mein kya difference hai? React kis type ka application hai?Medium
Answer:
SPA (Single Page Application): Ek hi HTML page load hota hai, content dynamically update hota hai JavaScript se. Page reload nahi hota. Better UX but SEO challenges.
MPA (Multi Page Application): Multiple HTML pages hain, har navigation pe new page load hota hai. Better SEO but slower navigation.
React primarily SPAs banane ke liye use hota hai, lekin Next.js jaise frameworks ke saath Server-Side Rendering bhi kar sakte hain jo SPA aur MPA ke benefits combine karta hai.
Transitions API: Non-urgent updates ko mark kar sakte hain using startTransition
Suspense for Data Fetching: Declarative loading states
New Root API: createRoot for concurrent features
Streaming SSR: Server-side rendering improvements
Concurrency: React ka ability multiple UI versions ko ek saath prepare karna. Main thread ko block nahi karta, urgent updates ko priority deta hai. Example: typing (urgent) aur search results update (non-urgent) ko alag handle karta hai.
Q6. Component-based architecture ke kya benefits hain? Explain with example.Medium
Answer: Component-based architecture ke benefits:
Reusability: Components ko multiple jagah use kar sakte hain
Maintainability: Isolated components ko maintain karna easy
Testability: Individual components ka testing simple
Scalability: Large applications ko organize karna easy
Separation of Concerns: Har component apni responsibility handle karta hai
Example: Ek Button component banao jo multiple forms mein reuse ho. Props se customization (color, size, text) control karo. Ek jagah change karo, sab jagah reflect ho.
Q7. React mein rendering process ko detail mein explain karo. Reconciliation kya hai?Hard
Answer: React Rendering Process:
Trigger Phase: Initial mount ya setState/props change se render trigger hota hai
Render Phase: React component functions ko call karta hai, JSX ko React elements mein convert karta hai, aur Virtual DOM tree bana
Reconciliation Phase: Previous Virtual DOM aur new Virtual DOM ko compare karta hai (Diffing Algorithm). Ye phase pure hai aur side-effects nahi hote
Commit Phase: Actual DOM mutations apply hote hain. useEffect/useLayoutEffect yahan run hote hain
Browser Paint: Browser updated UI display karta hai
Reconciliation: Ye process hai jismein React efficiently determine karta hai ki DOM mein kya changes chahiye. React keys aur element types use karke optimize karta hai. Fiber architecture isko interruptible banata hai.