Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access data returned from an axios get to display on a web page using react js?

Tags:

reactjs

axios

I have ajax functions in an api to which I'm making an axios get request from my react js component. How can I access the returned data to display it on the web page.

like image 883
LasyaPriya Avatar asked Sep 24 '16 20:09

LasyaPriya


1 Answers

Depends of what you trying to do but this is a example.

componentDidMount() {
  axios
    .get(`endpoint`)
    .then(res => this.setState({ posts: res.data }))
    .catch(err => console.log(err))
}

A good way too should be if you using react-router to make the ajax call with the onEnter api from the router.

like image 171
EQuimper Avatar answered Nov 15 '22 08:11

EQuimper