Last active
July 18, 2016 21:13
-
-
Save sverrejoh/dfc47649b24b604ad290d25d4ef741f3 to your computer and use it in GitHub Desktop.
React component with typed Refs
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
| // @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