I am trying to update the state by making api call in componentDidMount phase .
I need to do is to display the name of users . Few names are set in initial state which are working fine . Then i need to append more users which is returned by hitting a get api .
Pseudo code:
export class UserDetails extends Component {
constructor(props) {
super(props);
this.state = {
users: [
{
id: 1,
name: "aditya"
}
]
};
}
componentDidMount() {
this.jsonList();
}
jsonList() {
axios('https://api.myjson.com/bins/1fwkrw').then(function(response) {
this.setState((prevState) => {
{users: users.push(response.data.userdata)};
});
})
}
render() {
return (
<div className="users">
{this.state.users.map(user => {
return (
<div>
<div key={user.id}>{user.name}</div>
</div>
)
})}
</div>
);
}
}
But here i am getting error "Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined" . Any help where i am going wrong ?