Reputation: 19
This is the code I've written so far to get data using axios.
What am I doing wrong?
constructor(props) {
super(props);
this.state = {
user_list: []
};
}
componentDidMount() {
axios
.get("http://192.168.1.35:3012/user_list", {})
.then(function(response) {
var datalist = response.data[0];
this.state({ user_list: response.data });
})
.catch(function(error) {
console.log(error);
});
}
Upvotes: 1
Views: 717
Reputation: 112777
The function given to then
after your axios request doesn't have the this
value you expect.
You could e.g. make it into an arrow function to make it work:
componentDidMount() {
axios
.get("http://192.168.1.35:3012/user_list")
.then((response) => {
var datalist = response.data[0];
this.state({ user_list: response.data });
})
.catch((error) => {
console.log(error);
});
}
Upvotes: 1