How can I execute a function of the parent component in the child component according to the api response?
Parent:
import React from 'react';
import AjaxForm from './../AjaxForm'
const Add = () => {
const AddCommentDone=()=>{
console.log('Done')
}
const AddCommentFail=()=>{
console.log('Failed')
}
return (
<AjaxForm api='/api/Comment/Add'>
<input name='Comment' placeholder='Text' type='text'></input>
</AjaxForm>
)
}
export default Add
Child:
import React from 'react';
import axios from 'axios';
const AjaxForm = (props) => {
const handleSubmit=(e)=>{
axios.post(props.api, new FormData(e.target))
.then(function (response) {
console.log(response.data) //Api Response=> {message:'Thank you for your comment',callBack:'AddCommentDone'}
//run callback of api response on Parent
});
}
return (
<form onSubmit={handleSubmit}>
{props.children}
<button type='submit'>Submit</button>
</form>
)
}
export default AjaxForm
How can I run it in the parent with the name of the function from the api response object?