Next.js Discord

Discord Forum

Next.js: Issue with Recaptcha + React

Answered
Anay-208 posted this in #help-forum
Open in Discord
So I'm using recaptcha script, however, I'm facing a issue with useReducer

I'm sharing the most relevant part of the code:
  function onSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();

    if (!recaptchaReady) return;
    console.log("executed");
    if (!recaptchaRendered) {
      window.grecaptcha.render("recaptcha", {
        sitekey: process.env.NEXT_PUBLIC_RECAPTCHA_SITEKEY,
        callback: action === "signin" ? signin : signup,
        size: "invisible",
      });
      setRecaptchaRendered(true);
    } else {
      window.grecaptcha.reset();
    }
    window.grecaptcha.execute();
  }

Now, the functions signup/signup, uses a useReducer state. The state doesn't get updated for some reason, I need to rerender the recaptcha, which recaptcha doesn't allow. What can I do?
Answered by gin
i can heavily suggest u switchting to turnstile
View full answer

18 Replies

thats right
Any suggetsions on fixing this issue
i can heavily suggest u switchting to turnstile
Answer
I was thinking about it, however, Since I've already implemented it, I don't feel like switching for now
thats what i do
Oh alright, then, I guess I'll switch it later, I trust cloudflare docs will give me the easiest solution, thanks!
yeah np
its better
@gin a final question, I'm using turnstile invisible
      <Turnstile
      sitekey={process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY}
      onLoad={(widgetId, bound) => {
        bound.execute()
      }}
      onVerify={(token) => {
        console.log(token)
      }}
    />

So since its invisible, it'll be executed when user submits. So How do I execute it then? Should I store bound in a useState?