Reputation: 50
We are trying to use the combination of reCaptcha v2 and v3 in the login page so we explicit render the reCaptcha v2 widget(challenge based reCaptcha) if reCaptcha v3 (invisible reCaptcha) fails.
Here's the rough flow -
reCAPTCHA has already been rendered in this element
What I've tried -
I tried using the reset method when BE fails to verify the v2 token -
grecaptcha.reset();
but the reset method only uncheck the reCaptcha and doesn't de-render or destroy the reCaptcha.
I also tried programmatically removing the children of recaptcha container using .removeChild()
method in JavaScript but encountering the same error.
Please help me resolve this error?
Upvotes: 0
Views: 279
Reputation: 367
The error reCAPTCHA has already been rendered in this element, that you are encountering is because you are loading reCAPTCHA twice at the same element.
As per this Github issue :
The problem is that the DOM generated by
this.props.grecaptcha.render()
is not removed during<ReCAPTCHA
's unmount and therefore can cause trouble when rendering a new instance at the same place.I can see two possible solutions :
Implement
componentWillUnmount
to do following :Detach rendered DOM inside
componentWillUnmount
and save it into some global variable for later reuse. This prevents the component from slow reloading after each use, however different instances may need different rendering parameters so this has to be taken into account.
Refer to this Stack link and also check this Github issue for more information which might be helpful for you.
Upvotes: 1