Created
April 1, 2016 09:55
-
-
Save ochronus/79eef0210f4a3bad14336937cb96a8a5 to your computer and use it in GitHub Desktop.
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
| var ToggleText = React.createClass({ | |
| getInitialState: function () { | |
| return { | |
| showDefault: true | |
| } | |
| }, | |
| toggle: function (e) { | |
| // Prevent following the link. | |
| e.preventDefault(); | |
| // Invert the chosen default. | |
| // This will trigger an intelligent re-render of the component. | |
| this.setState({ showDefault: !this.state.showDefault }) | |
| }, | |
| render: function () { | |
| // Default to the default message. | |
| var message = this.props.default; | |
| // If toggled, show the alternate message. | |
| if (!this.state.showDefault) { | |
| message = this.props.alt; | |
| } | |
| return ( | |
| <div> | |
| <h1>Hello {message}!</h1> | |
| <a href="" onClick={this.toggle}>Toggle</a> | |
| </div> | |
| ); | |
| } | |
| }); | |
| React.render(<ToggleText default="World" alt="Mars" />, document.body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment