Skip to content

Instantly share code, notes, and snippets.

@juanmaguitar
Last active October 15, 2016 08:44
Show Gist options
  • Save juanmaguitar/7723cf41c3924132771102e1aa513b1e to your computer and use it in GitHub Desktop.
Save juanmaguitar/7723cf41c3924132771102e1aa513b1e to your computer and use it in GitHub Desktop.
Basic React Component
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First Component</title>
</head>
<body>
<!-- container node -->
<div id="app"></div>
<script src="http://fb.me/react-0.11.1.js"></script>
<script src="http://fb.me/JSXTransformer-0.11.1.js"></script>
<script type="text/jsx">
/** @jsx React.DOM */
var Timer = React.createClass({
getInitialState: function() {
return {secondsElapsed: 0};
},
tick: function() {
this.setState({secondsElapsed: this.state.secondsElapsed + 1});
},
componentDidMount: function() {
this.interval = setInterval(this.tick, 1000);
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
render: function() {
return (
<div>Seconds Elapsed: {this.state.secondsElapsed}</div>
);
}
});
React.renderComponent(
<Timer />,
document.getElementById('app')
);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment