React useRef Hook: Access DOM elements without re-render

Not everything in React needs state. Sometimes, you just need a reference. That’s where 👉 useRef comes in. 🔹 What is useRef? useRef is a React Hook used to: • Access DOM elements directly • Persist values between renders • Store mutable values without triggering re-render The important part? Updating a useRef value does NOT re-render the component. That’s what makes it different from useState. 💡 When Do We Use It? You typically use useRef for: • Focusing input fields • Handling timers • Storing previous values • Accessing DOM nodes • Integrating third-party libraries It’s more about control than UI updates. 🏢 Real-Time Example In a login form project, I used useRef to automatically focus the email input field when the page loaded. Instead of managing focus with state, I directly accessed the DOM element using useRef. Cleaner. Simpler. More efficient. 📌 Quick Interview Tip: useState → Triggers re-render useRef → Does NOT trigger re-render If the UI needs to update → useState If you just need a reference → useRef Tomorrow: I’ll explain React.memo in simple terms. If you're serious about mastering React fundamentals, stay connected 🚀 Saurav Singh #ReactJS #FrontendDevelopment #JavaScript #ReactDeveloper #LearningInPublic #WebDevelopment 🚀

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories