I want to avoid typing the same lines of code. Currently, I have an app that is supposed to make an API call, like so.
render: function(){
var processappkey = localStorage.getItem('yourid');
var weather = new XMLHttpRequest();
var deesfault = "Houston, Texas";
weather.open("GET", "http://apidatafromsomewebsiteq="+deesfault+"&units=imperial&appid="+processappkey, false);
weather.send(null);
var r = JSON.parse(weather.response);
var check = r.main.temp;
var theunicorn = r.weather[0].icon;
return (<p>{theunicorn}</p>)
}
I would like to split this up to something like this:
somecontainer: function(){
var processappkey = localStorage.getItem('yourid');
var weather = new XMLHttpRequest();
var deesfault = "Houston, Texas";
weather.open("GET", "http://apidatafromsomewebsiteq="+deesfault+"&units=imperial&appid="+processappkey, false);
weather.send(null);
var r = JSON.parse(weather.response);
var check = r.main.temp;
var theunicorn = r.weather[0].icon;
},
render: function() {
{this.somecontainer()}
return (
<p>{theunicorn}</p>
)
}
I will be calling the API from different areas in my app. Not to mention including a setInverval, which will have me repeating the code again.
As a matter of fact, while I am at it I would also like to know how to go about something like this.
render: function() {
this.somecontainer();
setInterval(function() {
this.somecontainer();
}, 5000);
}
However, that is a different question, and I'll be happy for insight on the first issue.