React JS 19 Tutorial in Hindi #38 useRef Hook
- What is useRef hook?
- Learn how to use useRef.
- Control input field with useRef.
- Hide and show input field with useRef.
- Interview Question
useRef
hook in React is used to create a mutable reference that:
- Persists across renders
- Does not trigger re-renders when changed
- Can reference a DOM element directly
import { useEffect, useState } from "react";
function App() {
const [counter,setCounter]=useState(0);
const [data,setData]=useState(0);
useEffect(()=>{
counterFunction();
},[counter])
useEffect(()=>{
callOnce
},[])
function counterFunction(){
console.log("counterFunction",counter);
}
function callOnce(){
console.log("callOnce function called");
}
return (
<div>
<h1>useEffect Hook</h1>
<button onClick={()=>setCounter(counter+1)} >Counter {counter}</button>
<button onClick={()=>setData(data+1)} >Data {data}</button>
</div>
)
}
export default App;