Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="app container">
	<h1>ES6 React Component lifecycle</h1>
	<div class="output">
		<div class="container">
			<div class="react col">
			</div>
			<br />
			<div class="scroll scrollbox">
				<table class="table table-striped table-bordered">
					<tbody class="list row"></tbody>
				</table>
			</div>
		</div>
	</div>
	<div class="container">
		<h2>Known React lifecycle events</h2>
		<h4>
			The React lifecycle will go through these cycles:</h4>
		<ul class="mono list list-unstyled list-inline">
			<li>Mounting</li>
			<li>Updating</li>
			<li>Unmounting</li>
		</ul>

		<table class="table table-striped table-bordered">
			<tr class="row">
				<th class="col-xs-3">Event</th>
				<th class="col-xs-1 center">ES5</th>
				<th class="col-xs-1 center">ES6</th>
				<th class="col-xs-5">Description</th>
				<th class="col-xs-3">When</th>
			</tr>
			<tr class="row">
				<td class="col-xs-12" colspan="5">Mounting</td>
			</tr>
			<tr class="row">
				<td class="col-xs-3">getInitialState</td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1 fail">&cross;</td>
				<td class="col-xs-5">Returns initial <strong>state</strong></td>
				<td class="col-xs-3">Once</td>
			</tr>
			<tr class="row">
				<td class="col-xs-3">getDefaultProps</td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1 fail">&cross;</td>
				<td class="col-xs-5">Returns initial <strong>props</strong></td>
				<td class="col-xs-3">Once (cached)</td>
			</tr>
			<tr class="row">
				<td class="col-xs-3"><a href="https://facebook.github.io/react/docs/react-component.html#componentwillmount" target="_blank">componentWillMount</a></td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1">&check;</td>
				<td class="col-xs-5">your before-component is mounted functionality</td>
				<td class="col-xs-3">Once</td>
			</tr>
			<tr class="row">
				<td class="col-xs-3"><a href="https://facebook.github.io/react/docs/react-component.html#render" target="_blank">render</a></td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1">&check;</td>
				<td class="col-xs-5">Returns HTML to render<br>
					<h5>Example from this component</h5>
					<pre>&lt;button className="btn btn-primary"
onClick={this.increment}&gt;
Clicked {this.state.count} time{(this.state.count&gt;1 || this.state.count===0) ? 's' : ''}
&lt;/button&gt;</pre>
				</td>
				<td class="col-xs-3">Every time</td>
			</tr>

			<tr class="row">
				<td class="col-xs-3"><a href="https://facebook.github.io/react/docs/react-component.html#componentdidmount" target="_blank">componentDidMount</a></td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1">&check;</td>
				<td class="col-xs-5">called after component has been rendered in DOM</td>
				<td class="col-xs-3">Once</td>
			</tr>
			<tr class="row">
				<td class="col-xs-12" colspan="5">Updating</td>
			</tr>
			<tr class="row">
				<td class="col-xs-3"><a href="https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops" target="_blank">componentWillReceiveProps</a></td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1">&check;</td>
				<td class="col-xs-5">called before render when <strong>props</strong> is about to change</td>
				<td class="col-xs-3">Prop changes</td>
			</tr>
			<tr class="row">
				<td class="col-xs-3"><a href="https://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate" target="_blank">shouldComponentUpdate</a></td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1">&check;</td>
				<td class="col-xs-5">stop rendering by returning <code>false</code></td>
				<td class="col-xs-3">Before render</td>
			</tr>
			<tr class="row">
				<td class="col-xs-3"><a href="https://facebook.github.io/react/docs/react-component.html#componentwillupdate" target="_blank">componentWillUpdate</a></td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1">&check;</td>
				<td class="col-xs-5">Just <em>before</em> <code>render</code> this method is ivoked</td>
				<td class="col-xs-3">Immediately before render</td>
			</tr>
			<tr class="row">
				<td class="col-xs-3"><a href="https://facebook.github.io/react/docs/react-component.html#componentdidupdate" target="_blank">componentDidUpdate</a></td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1">&check;</td>
				<td class="col-xs-5">Just <em>after</em> <code>render</code> this method is ivoked</td>
				<td class="col-xs-3">Immediately after render</td>
			</tr>
			<tr class="row">
				<td class="col-xs-12" colspan="5">Unmounting</td>
			</tr>

			<tr class="row">
				<td class="col-xs-3"><a href="https://facebook.github.io/react/docs/react-component.html#componentwillunmount" target="_blank">componentWillUnmount</a></td>
				<td class="center col-xs-1">&check;</td>
				<td class="center col-xs-1">&check;</td>
				<td class="col-xs-5">Just <em>before</em> the component is removed from DOM</td>
				<td class="col-xs-3">Once</td>
			</tr>

		</table>

	</div>

</div>
<div class="container">
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br /> &nbsp;
	</div>
              
            
!

CSS

              
                
@import url(https://fonts.googleapis.com/css?family=Inconsolata:400,700|Open+Sans:400,800);

* {
	box-sizing: border-box;
}
html,
body {
	width: 100vw;
font-family: 'Open Sans', sans-serif;
	margin: 0;
	padding: 0;
	background: #eee;
}

body {
	display: flex;
	flex-direction: row;
	justify-content: center;
	align-items: center;
}

.app, .container {
	width: 100%;
}
.render {
	background: lightgreen;
}


.scroll {
	height: 14em;
	max-height: 14em;
	overflow: auto;
	overflow-x: hidden;	
}
.user {
 color: grey;
}
.list {
	font-family: 'Inconsolata', monospace ;
}


a[target]:after {
	content: " ↗";
	color: black;
}

.center {
	text-align: center;
}

.fail {
	background: #efc7c7;
}

.list-inline li:not(:last-child):after {
	content: " → ";
}

.col-xs-12 {
	background: rgba(0,0,0,.1);
	border-width: 0;
	font-weight: 700;
}

code, pre, .mono {
	font-family: 'Inconsolata', monospace ;
}


.list {
	font-size: 14pt;
}


/* Borrowed from Lea Verou Pure CSS scrollbox 
http://lea.verou.me/2012/04/background-attachment-local/
*/
.scrollbox {
	overflow: auto;
	margin: 0 auto;

	background:
		/* Shadow covers */
		linear-gradient(white 30%, rgba(255,255,255,0)),
		linear-gradient(rgba(255,255,255,0), white 70%) 0 100%,
		
		/* Shadows */
		radial-gradient(50% 0, farthest-side, rgba(0,0,0,.2), rgba(0,0,0,0)),
		radial-gradient(50% 100%,farthest-side, rgba(0,0,0,.2), rgba(0,0,0,0)) 0 100%;
	background:
		/* Shadow covers */
		linear-gradient(white 30%, rgba(255,255,255,0)),
		linear-gradient(rgba(255,255,255,0), white 70%) 0 100%,
		
		/* Shadows */
		radial-gradient(farthest-side at 50% 0, rgba(0,0,0,.2), rgba(0,0,0,0)),
		radial-gradient(farthest-side at 50% 100%, rgba(0,0,0,.2), rgba(0,0,0,0)) 0 100%;
	background-repeat: no-repeat;
	background-color: white;
	background-size: 100% 40px, 100% 40px, 100% 14px, 100% 14px;
	
	/* Opera doesn't support this in the shorthand */
	background-attachment: local, local, scroll, scroll;
}
              
            
!

JS

              
                var list = document.querySelector('.list'),
	last = new Date(),
	threshold = 100,
	count = -1;
class Button extends React.Component {
	constructor(props) {
		super(props);
		console.log(arguments)
		this.events = [""];
		this.log("constructor", false, '(props, context, updater)')
		this.state = {
			count: 0
		};
	}
	log = (txt, user, args) => {
		var now = new Date();
		count++;
		var argsinfo = ((args) ? args.length : 0)+' arguments';
		txt = txt+args;
		if (now - last > threshold) {
			list.innerHTML = '<tr class="row"><td class="col-xs-12" colspan="3"><br></td></tr>' + list.innerHTML;
			last = now;
			count = 0;
		}
		list.innerHTML = '<tr class="row'+((user) ? ' user' : '')+'"><td class="col-xs-1">' + count + '</td><td class="col-xs-9">' + txt + '</td><td class="col-xs-2 mono">' + JSON.stringify(this.state) + '</td></tr>' + list.innerHTML;

	};

	componentWillMount = () => {
		this.log("componentWillMount", false, "()");
	};
componentWillUnmount = () => {
		this.log("componentWillUnmount", false, "()");
	};

	componentWillUpdate = () => {
		this.log("componentWillUpdate", false, "(nextProps, nextState, nextContext)");
	};
	componentDidUpdate = () => {
		this.log("componentDidUpdate", false, "(prevProps, prevState, prevContext)");
	};

	shouldComponentUpdate = () => {
		this.log("shouldComponentUpdate", false, "(nextProps, nextState, nextContext)");
		return true;
	};

	// Called only once 
	componentDidMount = () => {
		this.log("componentDidMount", false, "(prevProps, prevState, prevContext)")
	};

	// NOT CALLED IN ES6
	getInitialState = () => {
		this.log("getInitialState", false, "()");
	};
	// NOT CALLED IN ES6
	getDefaultProps = () => {
		this.log("getDefaultProps", false, "()");
	};
	// NOT CALLED IN ES6
	componentWillReceiveProps = () => {
		this.log("componentWillReceiveProps", false, "(nextProps)");
	};

	// This is a non-react-native function
	increment = () => {
		this.log("increment", true, "()")
		this.setState({
			count: this.state.count + 1
		});
	};
	render() {
		this.log("render", false, "()");
		return (
			<button className="btn btn-primary" onClick={this.increment}>Clicked {this.state.count} time{(this.state.count>1 || this.state.count===0) ? 's' : ''}</button>

		)
	}
}

var lifecycle = ReactDOM.render(
	<Button/>, document.querySelector('.react'));
              
            
!
999px

Console