I'm trying to implement invisible reCaptcha
from react-google-recaptcha in type script project
I added the package's type by
yarn add @types/react-google-recaptcha
but when I want to implement the component, I get a type script error in here
<ReCAPTCHA
ref={recaptchaRef} // IN HERE
size="invisible"
sitekey="Your client site key"
/>
and this is the error's content
Type 'MutableRefObject<undefined>' is not assignable to type 'LegacyRef<ReCAPTCHA> | undefined'.'' Type 'MutableRefObject<undefined>' is not assignable to type 'RefObject<ReCAPTCHA>'. Types of property 'current' are incompatible.
Just give it an initial value of
null
. It takesundefined
as initial value in your current implementation.Explanation:
By looking at types,
ref
attribute expects a type as below:i.e.
where
RefObject
is:So, the value of
current
should be of some type ornull
.