call child component method from parent react 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>);
}