Skip to content

Instantly share code, notes, and snippets.

@eferro

eferro/index.html

Created Jul 16, 2015
Embed
What would you like to do?
inject state to a react component from outside
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="content">
</div>
</body>
<script type="text/jsx">
var HelloComponent = React.createClass({
getInitialState: function () {
return {counter: 0};
},
render: function() {
return (
<div className="helloCoponent">
<h1>Counter { this.state.counter } </h1>
</div>
);
}
});
var reactComponent = React.render( <HelloComponent />, document.getElementById('content'));
var cont = 0;
setInterval(function(){
cont = cont + 1;
reactComponent.setState({counter: cont});
},1000);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment