The basics of React useRef Hook with Examples
Introduction
Hey there π, my name is Olix, and this is my first blog entry. Recently, useRef got introduced to us in AltSchool, and the explanation was so concise that I decided to share my thoughts.
What is useRef, first, before we continue?
In ver...
olix.com3 min read
Thanks ! and another example if we want to know how many times our Application renders with useState, we can't we get infinite loop cuz useState itself causes a re rendring, for that we can use useRef Hook
import { useState, useEffect, useRef } from "react"; function App() { const [inputValue, setInputValue] = useState(""); const count = useRef(0); useEffect(() => { count.current = count.current + 1; }); return ( <> <input type="text" value={inputValue} onChange={(e) => setInputValue(e.target.value)} /> <h1>Render Count: {count.current}</h1> </> ); }