I am trying to get data from a sample api, I keep getting the error:
Cannot read property 'setState' of undefined.
Can anyone please tell me why I get the undefined error, do I need to bind to this?
import React, { Component } from 'react';
import axios from 'axios';
import logo from './logo.svg';
import './App.css';
import '../node_modules/bootstrap/dist/css/bootstrap.css';
import '../node_modules/bootstrap/dist/css/bootstrap-theme.css';
import Users from './components/Users.js';
class App extends Component {
constructor(props) {
super(props);
this.state = {users: []};
}
getUsers(){
axios.get('http://jsonplaceholder.typicode.com/users')
.then(function (response) {
this.setState({ users: response.data });
})
.catch(function (error) {
console.log(error);
});
}
componentDidMount(){
this.getUsers();
}
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<Users data={this.state.users} />
</div>
);
}
}
export default App;