UseRef React TypeScript
const inputRef = React.useRef<HTMLInputElement | null>(null);
Obedient Oyster
const inputRef = React.useRef<HTMLInputElement | null>(null);
const input = React.useRef<HTMLInputElement>(null);
import { useRef, useLayoutEffect } from "react";
//HTMLInputElement || define the type of element
const element = useRef<HTMLInputElement>(null);
useLayoutEffect(() => {
//object can be null
if (element.current !== null) {
element.current.focus();
}
});
// create refernce for element
let refName= useState<HTMLDivElement>(null)
//usage
<div ref={refName} className=""></div>
// access in useEffect
refName.current
const element = React.useRef<ElementType>(null);