Using use object destructuring to destructe my state before sending a call to my REST API and it only works inside of an arrow function for me. I tried calling it inside of a regular function and I kept getting an error that it was undefined. Code examples are below
I'm calling the function in a child component, I'm not sure if that makes a difference. Your help would be greatly appreciated so I can learn this concept, thanks!
Code I don't understand why its breaking
async get() {
const { userData } = this.state
try {
const response = await http.get('/v1', {
userData
})
console.log('response', response);
await this.setState({friends: response.data});
} catch(err) {
console.log("error getting friends ", err);
}
}
**Code that works **
get = async () => {
const { userData } = this.state
try {
const response = await http.get('/v1', {
userData
})
console.log('response', response);
await this.setState({friends: response.data});
} catch(err) {
console.log("error getting friends ", err);
}
}