DIVYA PANDRAJU’s Post

⚛️ That moment when I just wanted to access an element... and React said “NO re-rendering allowed!” 😂 I was building a simple input box and needed to focus it when a button was clicked. Naturally, I thought: “Easy! I’ll use useState().” So I did something like this 👇 ❌ Before: function InputFocus() { const [input, setInput] = useState(null); const handleClick = () => { input.focus(); }; return ( <> <input ref={setInput} /> <button onClick={handleClick}>Focus</button> </> ); } And then React said: “Wait... you’re updating state just to get a DOM element?” 😩 That’s when I discovered useRef() — my quiet, reliable hook friend that doesn’t trigger re-renders. 🧘♂️ ✅ After using useRef(): function InputFocus() { const inputRef = useRef(null); const handleClick = () => { inputRef.current.focus(); }; return ( <> <input ref={inputRef} /> <button onClick={handleClick}>Focus</button> </> ); } 💡 Lesson learned: useRef() stores a mutable value that survives re-renders. Perfect for accessing DOM elements or saving values without causing updates. No unnecessary renders. No chaos. Just pure focus. 🎯 React hooks each have their own “superpower” — and useRef() is the stealthy one! 🦸♀️ #ReactJS #useRef #ReactHooks #FrontendDeveloper #MERNStack #WebDevelopment #LearningByDoing #JavaScript #ReactTips #CodingJourney

To view or add a comment, sign in

Explore content categories