I try to run child function in parent component.
I have a two child component with different functions. if active prop is true I try to call childOneFunction, otherwise, I should call childTwoFunction.
Here is my example code. What is the best way to do that?
const Parent = () => {
const [active, setActive] = useState(true);
function handleClickButton (){
if(active){
// call childOneFunction
}else{
// call childTwoFunction
}
}
return (
<div>
<ChildOne active={active}/>
<ChildTwo active={!active}/>
<button onClick={handleClickButton}>Click</button>
</div>
);
};
Child One component
const ChildOne = () => {
function childOneFunction() {
return 'Child One Clicked'
}
return (
<div>
<h1>{childOneFunction}</h1>
</div>
);
};
Child Two component
const ChildTwo = () => {
function childTwoFunction() {
return 'Child Two Clicked'
}
return (
<div>
<h1>{childTwoFunction}</h1>
</div>
);
};