From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance

Unlock this course with a free trial

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

useRef() hook

useRef() hook

Now, before moving on in the project, let's take a moment and learn an important hook of React called useRef hook as it is a prerequisite for the project ahead. The useRef is the React hook that lets you take a reference value, that is, a value which does not trigger a re-render. And not only that, it will also persist i.e. maintain the value as it is even on re-renders. It will take only one parameter which is called initial value. We can assign any type of value as the initial value and it will return an object which has only one property called current. The current has either initial value which we have defined or holds the JSX node if we pass the ref object to react as a ref attribute. Basically, it is there to get the reference of an element like we used to do with get element by id when working with JavaScript and DOM. Now let's see the usage of useRef practically. So I have created a counter in the project. I have taken a state using useState as the counter and the count will…

Contents