UseRef 2
UseRef:-
The useRef hook is primarily used for accessing and interacting with DOM elements.
The advantage of the useRef hook is that it remains consistent across renders and doesn't trigger re-renders when its value changes
import React, { useRef } from 'react';
function MyComponent() {
const inputRef = useRef(null);
const focusInput = () => {
if (inputRef.current) {
inputRef.current.focus();
}
};
return (
<div>
<h2>Using useRef Example</h2>
<input type="text" ref={inputRef} placeholder="Type something..." />
<button onClick={focusInput}>Focus Input</button>
</div>
);
}
export d
efault MyComponent;
Comments
Post a Comment