How to Build a Timer with useRef in React

SK— useRef (Build a Timer) 💡 Explanation: useRef holds values without causing re-renders. Great for timers, intervals & DOM elements. 🧩 Example: Timer function Timer() { const timerRef = useRef(null); const [seconds, setSeconds] = useState(0); const start = () => { timerRef.current = setInterval(() => setSeconds(s => s + 1), 1000); }; const stop = () => clearInterval(timerRef.current); return ( <> <h2>{seconds}s</h2> <button onClick={start}>Start</button> <button onClick={stop}>Stop</button> </> ); } 💬 Question: Have you ever used useRef to store previous values? 📌 Follow Sasikumar S for daily React reflections, dev career insights & coding inspiration. 🤝 Connect Now: sasiias2024@gmail.com 💟 Visit: sk-techland.web.app ❤️ Follow our LinkedIn Page for more React, JavaScript & software engineering growth. #ReactJS #WebDevelopment #FrontendDeveloper #JavaScript #SystemDesign #CodingJourney #SoftwareEngineering #ErrorHandling #GitFlow #ReactProjectStructure #CareerGrowth #MockInterview #TechLearning #ProblemSolving #DeveloperMindset

  • text

Great post! useRef is indeed invaluable for managing timers and preserving values without re-renders. Looking forward to more insights on React and development techniques!

Like
Reply

To view or add a comment, sign in

Explore content categories