I've been looking for a while on Stack but I cannot work out a solution.
I have one method that invoked should do a GET request with Axios.
At the beginning of the method I created a new hash , so I want to store all information there and then just return that hash.
import React, {Component} from "react";
import axios from 'axios';
class Json extends Component{
getInformation(){
let inf = {};
axios
.get("http://codepen.io/jobs.json")
.then(result=> {
inf.description = result.data.jobs[0].description;
})
.catch(err=>{
console.log("STH WRONG");
});
console.log(inf);
return(inf);
}
So what's my problem???? If I check what is inside the inf variable, it's empty. However, if I inspect with Chrome, on console I see it empty but when i check with detail the key and value are there. At the moment that i am trying to retrieve the information with inf.description
is undefined.
Any idea?? Feel free to try this example, just invoke this method to check it.
EDIT
import React, {Component} from 'react';
import axios from 'axios';
class Json extends Component{
constructor(props){
super(props);
this.state={title: ""};
}
getInformation(){
let inf = {};
// .get("/data/CA_data.json")
axios
.get("http://codepen.io/jobs.json")
.then(result=> {
inf.description = result.data.jobs[0].description;
})
.catch(err=>{
console.log("STH WRONG");
});
console.log(inf);
return(inf);
}
render(){
let inf = this.getInformation();
return(
<h1>
</h1>
);
}
}
export default Json;