what is useRef()? useRef ka use hota hai, kyunki wo value ko bina component
re-render kiye change kar sakta hai.agr ham bat kry
dosre hooks ki jese usestate, usestate jb current value ko update krta hn to wo rerender kr deta hn.
or useRef refrence dene ke liye kam ata hn ham is ko
const username = document.getElementById('username');
esy bhi kr skte hn but react me useRef ek acha option hn
Ye basically 2 kaam ke liye use hota hai:
1) Kisi DOM element ko directly control karna (e.g., kisi input ko focus karna ya value change karna).
2) Ek aisi value ko yaad rakhna jo re-render ke baad bhi reset na ho (lekin ye re-render trigger nahi
karta).
React-friendly: Virtual DOM ke sath seamlessly kaam karta hai.
Safer: Multiple components ke alag-alag scoped references manage karta hai.
Performance: DOM ke sath kaam karta hai bina re-render kare.
Jab application load hoti hai, React ek Virtual DOM banata hai jo asli DOM ka copy hota hai.Jab state ya props change hoti hain, React Virtual DOM ka naya version banata hai.React naya Virtual DOM aur purana Virtual DOM compare karta hai (diffing algorithm se).Ye changes ko identify karta hai.Sirf unhi parts ko update karta hai jo change hue hain (asli DOM ko minimum update karta hai)
What is Context API?Context API React ka ek feature hai jo global data ko manage aur share karne ke liye use hota hai bina har component me manually props pass kiye. Ye deeply nested components ke darmiyan data share karne ka efficient tareeqa hai.
Kyun Use Karte Hain?
Background Me Kaise Kaam Karta Hai?
1) React me createContext se ek global data container banta hai.
2) Top-level component (Provider) global data ko share karta hai.
3) Jo components data ko access karte hain, wo useContext hook ka use karte hain.
4) React efficiently re-renders sirf un components ko jo context data ko use karte hain.