Skip to content

Instantly share code, notes, and snippets.

@sverrejoh
Last active July 18, 2016 21:13
Show Gist options
  • Select an option

  • Save sverrejoh/dfc47649b24b604ad290d25d4ef741f3 to your computer and use it in GitHub Desktop.

Select an option

Save sverrejoh/dfc47649b24b604ad290d25d4ef741f3 to your computer and use it in GitHub Desktop.
React component with typed Refs
// @flow
import React from "react";
type Refs = {
// the ref `foo` is a button (or null, because it might not be mounted).
foo: HTMLButtonElement | null
};
class Foo extends React.Component {
// Annotating refs as an object of Refs
refs: Refs;
onClickHandler() {
// `foo` is a HTMLButtonElement.
if (this.refs.foo) {
console.log(this.refs.foo.innerHTML)
}
}
render() {
return <button ref="foo" onClick={this.onClickHandler}>Click</button>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment