Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of React component in Typescript using a callback to set a ref
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
You can’t perform that action at this time.