marsidev / react-turnstile

Cloudflare Turnstile integration for React.
https://www.npmjs.com/package/@marsidev/react-turnstile
MIT License
358 stars 21 forks source link

Ref to turnstile is always null #10

Closed imprakharshukla closed 1 year ago

imprakharshukla commented 1 year ago

Thank you for this library. I am trying to reset the captcha after a form submission, but the reference to the turnstile widget is always null.

const turnstileRef = useRef(null);

    <Turnstile
        ref={turnstileRef}
        sitekey="0x4AAAAAAACFaH4Vby0ghcqz"
        onVerify={(token) => setCaptchaToken(token)}
        onError={(error) => toast.error(error)}
        onExpire={onExpire}
        autoResetOnExpire={true}
     />

This is my resetting logic-

      function resetCaptcha() {
          if (turnstileRef.current) {
              turnstileRef.current?.reset()
          }
      }

      function onExpire() {
          setCaptchaToken(null)
          turnstileRef.current?.reset()
      }
marsidev commented 1 year ago

Hello!

Can you provide a repo or codesandbox with your error? Here is an example of a form with a turnstile widget, which resets after a form submission. With Next.js. Let me know if this helps you.