LogoBetter Captcha

SolidJS

Using better-captcha with SolidJS

Installation

npm i @better-captcha/solidjs

Basic Usage

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

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

Imperative Handle

SolidJS uses a controller pattern with createCaptchaController to access the imperative handle in a reactive way.

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

export default function Example() {
  const controller = createCaptchaController<ReCaptchaHandle>();

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

The controller's handle() function returns the handle once the widget is ready, or null if not yet initialized. Always use optional chaining (?.) when calling methods.

Lifecycle Callbacks

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

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

export default function Example() {
  const controller = createCaptchaController<ReCaptchaHandle>();

  const handleReady = (handle: ReCaptchaHandle) => {
    console.log("Captcha is ready", handle);
  };

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

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

  return (
    <ReCaptcha
      sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"
      controller={controller}
      onReady={handleReady}
      onSolve={handleSolve}
      onError={handleError}
    />
  );
}
CallbackParametersDescription
onReady(handle: Handle) => voidCalled when the widget is rendered and ready (receives handle)
onSolve(token: string) => voidCalled when the challenge is solved with the response token
onError(error: Error) => 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 { ReCaptcha, createCaptchaController } from "@better-captcha/solidjs/provider/recaptcha";

export default function Example() {
  const controller = createCaptchaController();

  return (
    <>
      <ReCaptcha 
        sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI" 
        controller={controller} 
        autoRender={false} 
      />
      <button onClick={async () => await controller.handle()?.render()}>Show Captcha</button>
    </>
  );
}

Common Props

PropTypeRequiredDescription
sitekeystringYour provider's site key
controllerControllerController from createCaptchaController() for imperative access
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/solidjs/provider/recaptcha
HCaptcha@better-captcha/solidjs/provider/hcaptcha
Turnstile@better-captcha/solidjs/provider/turnstile
Friendly Captcha@better-captcha/solidjs/provider/friendly-captcha
Private Captcha@better-captcha/solidjs/provider/private-captcha
Captcha Fox@better-captcha/solidjs/provider/captcha-fox
Prosopo@better-captcha/solidjs/provider/prosopo
CapWidget@better-captcha/solidjs/provider/cap-widget

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

On this page