How do I set the state of a get response in axios?
axios.get(response){ this.setState({events: response.data}) }
First, you import React and Axios so that both can be used in the component. Then you hook into the componentDidMount lifecycle hook and perform a GET request. You use axios. get(url) with a URL from an API endpoint to get a promise which returns a response object.
To change a value in the state object, use the this. setState() method. When a value in the state object changes, the component will re-render, meaning that the output will change according to the new value(s).
To use async and await with Axios in React, we can call axios in an async function. We call axios with the URL we want to make a GET request to. It returns a promise that resolves to an object with the data property set to the response data. So we use await to return the resolved value from the promise.
You have a syntax error here. You should try this instead
var self = this; axios.get('/url') .then(function (response) { console.log(response); self.setState({events: response.data}) }) .catch(function (error) { console.log(error); }); //the rest of the code var a = 'i might be executed before the server responds'
There are a few things to note here:
axios.get
is an asynchronous function which means that the rest of the code will be executed .And when the response of the server arrives, the function passed to then
will be executed. The return value of axios.get('url')
is called a promise object. You can read more about it here this
keyword has a different value depending of where it is called. this
in this.setState
should refer to the constructor object, and when you call this
inside a function, it refers to the window
object. That is why i assigned this
to the variable self
. You can read more about this here Pro tip:
If you use ES6, you would want to use arrow functions (which don't have their own this
) and use this.setState
without assigning this
to a variable. more about it here
axios.get('/url') .then((response) => { console.log(response); this.setState({events: response.data}) }) .catch((error)=>{ console.log(error); });
Here is a complete example https://codesandbox.io/s/rm4pyq9m0o containing best practices commonly used to fetch data including error handling, try again and loading. This provides a better User experience. You are encouraged to modify the code and play around to get more insights about it.
This isn't working because "this" is different inside of axios. "this" inside axios refers to the axios object, not your react component. You can resolve this with .bind
Also axios isnt being used properly.
it should look something like
axios.get("/yourURL").then(function(response) { this.setState({ events: response.data }); }.bind(this));
Alternatively if using es6 you could sub out the function for an arrow function and get the same effect without bind
axios.get("/yourURL").then(response => { this.setState({ events: response.data }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With