Skip to content

Instantly share code, notes, and snippets.

@brainkim
Created October 21, 2013 22:39
Show Gist options
  • Save brainkim/7092130 to your computer and use it in GitHub Desktop.
Save brainkim/7092130 to your computer and use it in GitHub Desktop.
Dream-code for react component and flight component baby.
var LikeButton = React.defineClass(LikeButton, withAnimation); //withAnimation is a mixin
//The LikeButton function is called on the React component prototype.
function LikeButton() {
//initialstate would be a method that is already mixed into the component.
this.initialState({
liked: false
});
this.defaultProps({
likeText: 'like',
unlikeText: 'unlike'
});
// I also think that flight has solved the same problem that you guys faced with having to 'autobind' methods.
this.toggle = function() {
this.setState({liked: !this.state.liked});
};
this.render = function() {
var text = this.state.liked ? this.props.unlikeText : this.props.likeText;
return (
<p onClick={this.toggle}>
{text}
</p>
);
};
// What the after method does is look for a mount method on the component. If it is
// a function, it wraps the function in place, making sure the second argument is called
// with the same this keyword and arguments after the original mount method is invoked.
this.after('mount', function(node) {
//From withAnimation mixin
this.animate(node);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment