Created
October 15, 2016 08:44
-
-
Save juanmaguitar/637c1de7cf42c3a9c7381658c14753a2 to your computer and use it in GitHub Desktop.
React component change state (basic example)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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