Created
November 27, 2014 02:29
-
-
Save yang-wei/11d935161fe490c89d6b to your computer and use it in GitHub Desktop.
React JS Component LifeCycle http://jsfiddle.net/yangweilim/kb3gN/8159/
This file contains 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
<div class="container"> | |
<div class="row"> | |
<div class="col-xs-12"> | |
<button class="btn btn-primary" onClick="render()">Render</button> | |
<button class="btn btn-danger" onClick="unmount()">Unmount</button> | |
</div> | |
<br/><br/> | |
<div class="col-xs-12" id="react"></div> | |
</div> | |
</div> | |
<script src="http://fb.me/react-js-fiddle-integration.js"></script> |
This file contains 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
/** @jsx React.DOM */ | |
var Number = React.createClass({ | |
getDefaultProps: function() { | |
console.log("getDefaultProps") | |
return { | |
val: '' | |
} | |
}, | |
getInitialState: function() { | |
console.log("getInitialState"); | |
return { | |
factor: '' | |
} | |
}, | |
increment: function() { | |
this.setProps({ | |
val: ++this.props.val | |
}); | |
}, | |
componentWillMount: function() { | |
console.log("componentWillMount"); | |
}, | |
render: function() { | |
console.log("render"); | |
return ( | |
<button className="btn btn-info" onClick={this.increment}>{this.props.val}</button> | |
) | |
}, | |
componentDidMount: function() { | |
console.log("componentDidMount"); | |
}, | |
componentWillReceiveProps: function(nextProps) { | |
/* update state and prop here */ | |
console.log("componentWillUpdate with param " + nextProps); | |
}, | |
shouldComponentUpdate: function(nextProps, nextState) { | |
/* return false to skip render, componemtWillUpdate and componentDidUpdate will be skipped too */ | |
console.log("shouldComponentUpdate"); | |
/* true is default */ | |
return true; | |
}, | |
componentWillUpdate: function(nextProps, nextState) { | |
/* do something to respond to state change here, can't use this.setState() here */ | |
console.log("componentWillUpdate"); | |
}, | |
componentDidUpdate: function(prevProps, prevState) { | |
console.log("componentDidUpdate"); | |
}, | |
componentWillUnmount: function() { | |
console.log("componentWillUnmount"); | |
} | |
}); | |
window.render = function() { | |
React.render( | |
<Number val={0} />, | |
document.getElementById("react") | |
) | |
} | |
window.unmount = function() { | |
React.unmountComponentAtNode(document.getElementById("react")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this isn't working bruh