I'm having a problem when using forwardRef with TypeScript.
What I want? pass a typed ref to the child component.
What am I getting
value of console.log - child component null
Code for parent component
// component parent
import { FormHandles } from '@unform/core';
export const App = () => {
const formRef = createRef<FormHandles>();
return (
<Child ref={formRef} />
)
}
Code for child component
// component child
import { FormHandles } from '@unform/core';
export const Child = forwardRef<FormHandles>((props, ref) => {
console.log('child component', ref.current);
return (
<div>any thing</div>
)
})
useRefhook inAppto create (and use i.e. access that was already created) the ref. Why is itnull? It depends on your code. It might also beundefinedbecause you gave it no initial value increateRef<FormHandles>();so, from where should it get the value?