LogoBetter Captcha

React

Using better-captcha with React

Installation

npm i @better-captcha/react

Basic Usage

import { ReCaptcha } from "@better-captcha/react/provider/recaptcha";

export default function Example() {
  return <ReCaptcha sitekey="your-site-key" />;
}

Imperative Handle

React uses refs with useRef to access the imperative handle for programmatic control.

import { useRef } from "react";
import { ReCaptcha, type ReCaptchaHandle } from "@better-captcha/react/provider/recaptcha";

export default function Example() {
  const recaptchaRef = useRef<ReCaptchaHandle>(null!);

  return (
    <>
      <ReCaptcha sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI" ref={recaptchaRef} />
      <button onClick={() => recaptchaRef.current.execute()}>Execute</button>
      <button onClick={() => recaptchaRef.current.reset()}>Reset</button>
      <button onClick={() => console.log(recaptchaRef.current.getResponse())}>Log response</button>
    </>
  );
}

The handle is available as soon as the widget is rendered. Until then, a safe no-op handle is exposed to maintain type safety.

Lifecycle Callbacks

All captcha components support three lifecycle callbacks for handling different stages of the widget lifecycle.

import { ReCaptcha } from "@better-captcha/react/provider/recaptcha";

export default function Example() {
  const handleReady = () => {
    console.log("Captcha is ready");
  };

  const handleSolve = (token: string) => {
    console.log("Captcha solved with token:", token);
    // Send token to your backend for verification
  };

  const handleError = (error: Error | string) => {
    console.error("Captcha error:", error);
  };

  return (
    <ReCaptcha 
      sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"
      onReady={handleReady}
      onSolve={handleSolve}
      onError={handleError}
    />
  );
}
CallbackParametersDescription
onReady() => voidCalled when the widget is rendered and ready for interaction
onSolve(token: string) => voidCalled when the challenge is solved with the response token
onError(error: Error | string) => voidCalled when an error occurs during initialization or solving

Manual Rendering

By default, captchas automatically render on mount. For more control, disable auto-rendering and trigger it manually.

import { useRef } from "react";
import { ReCaptcha } from "@better-captcha/react/provider/recaptcha";

export default function Example() {
  const recaptchaRef = useRef(null);

  return (
    <>
      <ReCaptcha 
        sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI" 
        ref={recaptchaRef} 
        autoRender={false} 
      />
      <button onClick={() => recaptchaRef.current.render()}>Show Captcha</button>
    </>
  );
}

Common Props

PropTypeRequiredDescription
sitekeystringYour provider's site key
optionsobjectProvider-specific configuration options
autoRenderbooleanWhether to render automatically on mount (default: true)
scriptOptionsobjectScript loading configuration (see Script Options)
onReadyfunctionCallback when widget is ready
onSolvefunctionCallback when challenge is solved
onErrorfunctionCallback when an error occurs

Handle Methods

All providers implement these methods on the handle:

Prop

Type

Available Providers

ProviderImport Path
ReCaptcha@better-captcha/react/provider/recaptcha
HCaptcha@better-captcha/react/provider/hcaptcha
Turnstile@better-captcha/react/provider/turnstile
Friendly Captcha@better-captcha/react/provider/friendly-captcha
Private Captcha@better-captcha/react/provider/private-captcha
Captcha Fox@better-captcha/react/provider/captcha-fox
Prosopo@better-captcha/react/provider/prosopo
CapWidget@better-captcha/react/provider/cap-widget

See the Provider Documentation for provider-specific options and configuration.

On this page