1

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);
    });
}
Tholle
  • 108,070
  • 19
  • 198
  • 189
  • Possible duplicate of [React Native setState not a function](https://stackoverflow.com/questions/51098867/react-native-setstate-not-a-function) – RIYAJ KHAN Jul 10 '18 at 11:35
  • https://stackoverflow.com/questions/51189438/difference-between-binding-a-method-and-not/51189571#51189571 check this one – RIYAJ KHAN Jul 10 '18 at 11:36

1 Answers1

1

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);
    });
}
Tholle
  • 108,070
  • 19
  • 198
  • 189