Skip to content

Instantly share code, notes, and snippets.

@yang-wei
Created November 27, 2014 02:29
Show Gist options
  • Save yang-wei/11d935161fe490c89d6b to your computer and use it in GitHub Desktop.
Save yang-wei/11d935161fe490c89d6b to your computer and use it in GitHub Desktop.
React JS Component LifeCycle http://jsfiddle.net/yangweilim/kb3gN/8159/
<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>
/** @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"));
}
@yang-wei
Copy link
Author

Fiddle. Open dev tools to see the console

@DuudeXX8
Copy link

this isn't working bruh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment