So I have a react function that is a api call ad gets some data, I want to just return the status of that back to the parent component.
Here is my code
Parent component function
JavaScript
x
6
1
//this is called via a button with a state passed down to the function
2
async function scheduleParent(){
3
const returned = await ScheduleChild(data)
4
console.log(returned)
5
}
6
Child function
JavaScript
1
13
13
1
export default async function ScheduleChild(data){
2
await axios({
3
method: "POST",
4
url: //myapi,
5
data: {data}
6
}).then(res => {
7
console.log(res)
8
return(res)
9
}).catch(err => {
10
console.log(err)
11
return(err)
12
});
13
I am getting undefined when console.log(returned) is called.
Advertisement
Answer
You are returning from the callback function and not from the SchedulePayment
. You should add return before your api call in order to work. Or in a nicer way
JavaScript
1
10
10
1
export default async function SchedulePayment(data){
2
try {
3
const res = axios.post(<api-url>, data)
4
return res
5
} catch (err) {
6
console.log(err)
7
return(err)
8
}
9
}
10