Skip to content

Instantly share code, notes, and snippets.

@carlosdlf
Created May 1, 2015 00:46
Show Gist options
  • Save carlosdlf/815d4066a56f7dcc7de3 to your computer and use it in GitHub Desktop.
Save carlosdlf/815d4066a56f7dcc7de3 to your computer and use it in GitHub Desktop.
Mount and unmount - reactjs
/**
* Created by clarico on 30/04/2015.
*/
var Button = React.createClass({
getInitialState: function () {
return {val : 0}
},
update : function () {
this.setState({val: this.state.val +1});
},
componentWillMount: function () {
console.log('mounting')
},
render:function(){
console.log("rendering...");
return <button onClick={this.update}>{this.state.val}</button>
},
componentDidMount : function () {
console.log('mounted');
},
componentWillUnmount : function () {
console.log('bye!')
}
});
var App = React.createClass({
mount : function () {
React.render(<Button />, document.getElementById("app"));
},
unmount : function () {
React.unmountComponentAtNode(document.getElementById("app"));
},
render:function(){
return (
<div>
<button onClick={this.mount}>Mount</button>
<button onClick={this.unmount}>UnMount</button>
<div id="app"></div>
</div>
)
}
});
React.render(<App/>,document.body );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment