Created
July 30, 2019 20:03
-
-
Save ChrisDobby/de57c0e1c103780c41232a0ed29c7f2f to your computer and use it in GitHub Desktop.
Example of React component in Typescript using forwarding refs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
It's complicated but here's how you do it: https://stackoverflow.com/questions/62238716/using-ref-current-in-react-forwardref