Skip to content

Instantly share code, notes, and snippets.

@juanmaguitar
Created October 15, 2016 08:44
Show Gist options
  • Save juanmaguitar/637c1de7cf42c3a9c7381658c14753a2 to your computer and use it in GitHub Desktop.
Save juanmaguitar/637c1de7cf42c3a9c7381658c14753a2 to your computer and use it in GitHub Desktop.
React component change state (basic example)
<!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