Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of React component in Typescript using forwarding refs
import * as React from "react";
const Forward = React.forwardRef((props, ref: React.Ref<HTMLDivElement>) => (
<div ref={ref} style={{ width: "100%", height: "30px", backgroundColor: "green" }} />
function ForwardRef() {
const divRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
if (divRef.current) {
console.log(`forwardRefRef div width: ${divRef.current.clientWidth}`);
return <Forward ref={divRef} />;
export default ForwardRef;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment