react useref input value code example
Example 1: useRef() in react
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
Example 2: how to add value with useref in react
useEffect(() => {
if (titleRef.current) titleRef.current.value = item?.title;
if (desRef.current) desRef.current.value = item?.description;
});