Skip to content

Instantly share code, notes, and snippets.

@ochronus
Created April 1, 2016 09:55
Show Gist options
  • Select an option

  • Save ochronus/79eef0210f4a3bad14336937cb96a8a5 to your computer and use it in GitHub Desktop.

Select an option

Save ochronus/79eef0210f4a3bad14336937cb96a8a5 to your computer and use it in GitHub Desktop.
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