batched updates in React component lifecycle

by koba04

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
<script src="https://fb.me/react-with-addons-15.0.1.js"></script>
<script src="https://fb.me/react-dom-15.0.1.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>

<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

JavaScript 1.7

var renderCount = 0;
var Hello = React.createClass({
	getInitialState: function() {
  	this.renderCount = 0;
    this.updated = false;
  	return {count: 0};
  },
	componentDidUpdate: function() {
  	if (!this.updated) {
   		this.updated = true;
      this.setState({count: this.state.count + 1});
      this.setState({count: this.state.count + 1});
      this.setState({count: this.state.count + 1});    
    }
	},
	render: function() {
    return (
    	<div>
      	<div>StateCount:{this.state.count}</div>
      	<div>RenderCount:{++this.renderCount}</div>
			</div>
    );
  }
});

ReactDOM.render(
  <Hello />,
  document.getElementById('container')
);
ReactDOM.render(
  <Hello />,
  document.getElementById('container')
);