Closed colorninja closed 2 years ago
Since I am only using one key, I am currently using a ugly workaround, but hey - it works!
import {useGoogleReCaptcha} from "react-google-recaptcha-v3";
export const useRecaptchaWrapper = () => {
const {executeRecaptcha} = useGoogleReCaptcha();
const globalRecaptcha = (action: string) => window.grecaptcha?.execute(process.env.RECAPTCHA_SITE_KEY, {action});
return {
executeRecaptcha: executeRecaptcha || globalRecaptcha,
};
}
Hi @colorninja, the lib is designed to work with SPA. The job of the provider is to inject a script to the site and listen on the onload event of that script to get the execute function. It's built to have one provider instance per page.
I will consider the approach you propose and how this lib can address the situations when there needs to be more than one instance per page, but it's not something I can guarantee that will happen.
@t49tran
<root>
<content>
{ Authorize ?
<GoogleReCaptchaProvider reCaptchaKey={process.env.RECAPTCHA_SITE_KEY}>
{children}
</GoogleReCaptchaProvider>
:
<>
{children}
</>
</content>
<Modal>
{ condition &&
<GoogleReCaptchaProvider reCaptchaKey={process.env.RECAPTCHA_SITE_KEY}>
<dynamicComponent/>
</GoogleReCaptchaProvider>
</Modal>
</root>
having same problem
TypeError: executeRecaptcha is not a function
Issue description
When you try to use multiple providers, some of the providers might no receive the execute function because of race condition.
I am using this on pages where react is not a full SPA yet, so it's broken down into components. Therefore I can not use the same provider and am forced to use more than one provider.
Steps to reproduce the issue
const elements: EmbeddedElements[] = [ {component: withGoogleRecaptcha(Newsletter), selector: '#newsletter-react'}, {component: withGoogleRecaptcha(AnotherFormModal), selector: '.form-modal-react'}, ];
elements.map(({selector, component: Component}) => { const items = document.querySelectorAll(selector);
});