useState vs useRef: a tricky React interview question

📰 Medium · Programming

Learn to use useState and useRef in React to display previous and current counter values

intermediate Published 26 Jun 2026
Action Steps
  1. Create a React counter component using useState to store the current count
  2. Use useRef to store the previous count value
  3. Update the previous count value in the useRef on each render
  4. Display both the current and previous count values in the component
  5. Test the component to ensure it works as expected
Who Needs to Know This

React developers and frontend engineers can benefit from understanding the difference between useState and useRef to make informed decisions in their projects

Key Insight

💡 useState triggers re-renders on value changes, while useRef persists values between renders without triggering re-renders

Share This
💡 Use useState for state changes and useRef for persistent values that don't trigger re-renders #React #useState #useRef

Key Takeaways

Learn to use useState and useRef in React to display previous and current counter values

Full Article

The task looks trivial: here’s a counter — now also show its previous value. Continue reading on Medium »
Read full article → ← Back to Reads