Here I have a parent component with a button and a child component with a function to show an alert.
but I was getting this error,
Function components cannot be given refs. Attempts to access this ref will fail.
code:
import { useRef } from "react"
const ChildComp = () => {
function showAlert() {
alert("Hello from Child Component")
}
return <div></div>
}
function App() {
const childCompRef = useRef()
return (
<div>
<button>Click Me</button>
<ChildComp ref={childCompRef} />
</div>
)
}
export default App
What's the issue?