Created
July 30, 2019 20:02
-
-
Save ChrisDobby/20fec6c834443ada50511284bd21a2c1 to your computer and use it in GitHub Desktop.
Example of React component in Typescript using a callback to set a ref
This file contains hidden or 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"; | |
class CallbackRef extends React.Component { | |
divRef: HTMLDivElement | null = null; | |
setDivRef = (element: HTMLDivElement) => { | |
this.divRef = element; | |
}; | |
componentDidMount() { | |
if (this.divRef) { | |
console.log(`callbackRef div width: ${this.divRef.clientWidth}`); | |
} | |
} | |
render() { | |
return <div ref={this.setDivRef} style={{ width: "100%", height: "30px", backgroundColor: "red" }} />; | |
} | |
} | |
export default CallbackRef; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment