react how trigger a function in child from parent with createRef code example
Example: react hooks call child fuynction
export default function Parent() {
const myref=useRef();
const handleOnClick = () => {
if(myref.current) {
myref.current.sayHi();
}
}
return (
<Child ref={myref} />
<button onClick={handleOnClick}>Click me</button>
);
}
export default function Child() {
const sayHi = () => {
console.log('hi');
};
return (<div>Hello</div>);
}