LasyaPriya LasyaPriya - 1 month ago 17
React JSX Question

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

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.

Answer

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.

Comments