const Parent = (props) => {
return <Child {...props} />
};
const Child = (props) => {
function handleClick() {
...
}
return <div {...props}><a onClick={handleClick}>link</a></div>;
}
I want to call the child's handleClick
function from within its parent component (that has some custom events). My first guess was to use a state in a parent component and invoke the handleClick
event in a useEffect
callback:
const Parent = (props) => {
const [toggle, setToggle] = useState(false);
return <Child toggle={toggle} {...props} />
};
const Child = (props) => {
useEffect(() => { if (toggle) handleClick() }, [toggle]);
function handleClick() {
...
}
return <div {...props}><a onClick={handleClick}>link</a></div>;
}
However I believe this is not what useEffect
is created for and is a poor solution all around.