Created
October 21, 2013 22:39
-
-
Save brainkim/7092130 to your computer and use it in GitHub Desktop.
Dream-code for react component and flight component baby.
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 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