Created
October 10, 2015 23:42
-
-
Save mattmccray/cd3b248b0d2a9d769689 to your computer and use it in GitHub Desktop.
ReactComponentBeforeAndAfter.ts - Revised
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
| // The 'After' revised: | |
| import * as React from "react"; | |
| // The T in React.Props<T> is used for the function style access of `ref` | |
| // for use by the caller. Example (defines `component` parameter of callback): | |
| // <Button ref={ component => this._button = component }/> | |
| interface ButtonProps extends React.Props<Button> { | |
| icon : string; | |
| caption : string; | |
| handler : ()=>boolean; | |
| onCloseRequest? : ()=>void; | |
| } | |
| class Button extends React.Component<ButtonProps,{}> { | |
| render() { | |
| let className = `fa ${ this.props.icon }`; | |
| return ( | |
| <button type="button" className="btn btn-primary" onClick={this.handleClick}> | |
| <span className={className} /> {this.props.caption} | |
| </button> | |
| ); | |
| } | |
| // Using this syntax, `this` is pre-bound. Bonus, if you want the event | |
| // object, you'd use `React.MouseEvent` (instead of `MouseEvent`). | |
| private handleClick = (e:React.MouseEvent) => { | |
| let shouldClose = this.props.handler(); | |
| if (shouldClose) | |
| { | |
| this.props.onCloseRequest(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment