Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
Created July 30, 2019 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisDobby/ae5f22fcce5388fd54512acac5adaaa2 to your computer and use it in GitHub Desktop.
Save ChrisDobby/ae5f22fcce5388fd54512acac5adaaa2 to your computer and use it in GitHub Desktop.
Example of React component in Typescript using createRef
import * as React from "react";
class CreateRef extends React.Component {
divRef = React.createRef<HTMLDivElement>();
componentDidMount() {
if (this.divRef.current) {
console.log(`createRefRef div width: ${this.divRef.current.clientWidth}`);
}
}
render() {
return <div ref={this.divRef} style={{ width: "100%", height: "30px", backgroundColor: "blue" }} />;
}
}
export default CreateRef;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment