Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
Created July 30, 2019 19:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ChrisDobby/e0ba079486625470e08e72a551156469 to your computer and use it in GitHub Desktop.
Save ChrisDobby/e0ba079486625470e08e72a551156469 to your computer and use it in GitHub Desktop.
Example of React component in Typescript using the useRef hook
import * as React from "react";
function HookRef() {
const divRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
if (divRef.current) {
console.log(`hookRef div width: ${divRef.current.clientWidth}`);
}
}, []);
return <div ref={divRef} style={{ width: "100%", height: "30px", backgroundColor: "orange" }} />;
}
export default HookRef;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment