Answers for "react hooks call child fuynction"

0

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>);
}
Posted by: Guest on January-28-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language