I tried to call a function in a child component from a parent component in React, but without the use of refs, and i succeeded but i don't know if my way is something other than a cheap hack. What can be a better way to do this?
I'm also reading the docs but i may have missed the right way. Any resource suggestion and idea is appreciated.
My parent code:
function Parent(props){
const [submitClicked, setSubmitClicked] = useState(false);
function onSubmitClick(e){
e.preventDefault();
setSubmitClicked(true);
}
return (
<Child
submitClicked={submitClicked}
setSubmitClicked={setSubmitClicked}
/>
);
}
Child code:
function Child(props) {
useEffect(()=>{
if(props.submitClicked){
//do something in the child component...
props.setSubmitClicked(false);
}
});
}