Skip to content

Instantly share code, notes, and snippets.

@leonardoo
Created August 9, 2016 14:42
Show Gist options
  • Save leonardoo/5744050a7e2c627b286b34a1158727a4 to your computer and use it in GitHub Desktop.
Save leonardoo/5744050a7e2c627b286b34a1158727a4 to your computer and use it in GitHub Desktop.
const InputBox = React.createClass({
getInitialState: function() {
return {data: [], interval: undefined};
},
componentDidMount: function() {
const inverval = setInterval(this.update, 5000);
this.setState({inverval});
},
componentWillUnmount: function() {
clearInterval(this.state.inverval);
this.setState({interval:undefined});
},
update: function(){
const {data} = this.state;
fetch('http://localhost:8181/data')
.then((response) => {
if(response.ok){
this.setState({data: response.json()})
}else{
console.log(response);
}
})
.catch(function(error) {
console.log(error.message);
});
},
render: function() {
return (
<div className={"commentBox"}>
<div>
<div>
<input />
</div>
<button>Enviar</button>
</div>
<div>
{this.state.data.map((data)=>{
return (<p>{data}</p>);
})}
</div>
</div>
);
}
});
ReactDOM.render(
<InputBox />,
document.getElementById('contenido')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment