I have a parent class component and a child functional component. How can we pass value from this type of child component to parent component - I have seen some examples that passes value from a child class component to parent class component.
Parent component
import React from "react";
import ChildComponent from "./ChildComponent/ChildComponent";
class ParentComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [] // An index value is in this state
};
}
render(){
//can console log the return value from the ChildComponent here if needed
return(
<React.Fragment>
<ChildComponent indexval = {this.state.data.index} />
</React.Fragment>
)
}
}
export default ParentComponent;
A prop indexval is passed down to the child component which is a functional component as below
Child component
import React from 'react';;
import Button from './Button/Button';
function ChildComponent(props) {
const newVal=(index) => {
let retVal = index + 1; //the retValue need to pass to the parent component
}
return (
<React.Fragment>
<Button onClick={() => newVal(props.index)}>Click</Button>
</React.Fragment>
)
}
As you can notice that the ChildComponent has a value retVal that need to be passed to the parent component. How can we achieve this