From the course: Master React 19 and Next.js 16 with Hands-On Projects and Real-World Applications

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Introduction

Introduction

In this session, I'll guide you through a basic example to get you acquainted with React refs. We'll build a simple click counter, but the real magic happens when we incorporate a ref to track the click count outside of React's state management. Let's start with our basic React template. Ok, let's add some state management using the useState hook. I've imported useState from React. And we now have a clicks state variable, initialized to 0, and a setClicks function to update it. This lays the groundwork for our click counter, as we'll be able to change the value of this variable and cause re-renders. Now I am adding some interactivity to your component. I am now returning a div which wraps a button and a paragraph. The paragraph displays the current value of clicks. This shows how we will visually represent the state of our application to the user. Now it is time to add the functionality to our button. I've created a handleClick function that increments the clicksState variable…

Contents