I'm trying to fetch a json and include it in my state.
Strangely when I launch my ajax
request with axios
inside a componentDidMount
and I console.log
the state updated inside the render method it returns an empty object, then the state updated.
I just want to remove this empty object. How can I do ?
Here is the code :
componentDidMount() {
axios({
method: 'get',
baseURL: '/url',
headers: {
"foo": "bar",
"key": "tata"
},
timeout: 3000,
responseType: 'json'
}).then(response => {
const posts = response.data.blocks.map(post => {
if (typeof post.description !== 'undefined' && typeof post.description !== '') {
return post;
} else {
return;
}
});
this.setState({posts});
});
}