--- title: React SDK --- # React SDK Official React component + hook for CaptchaLa CAPTCHA — published as [`@captcha-la/react`](https://www.npmjs.com/package/@captcha-la/react). ## Live demo ::: tip 📦 [demo-v1.captcha.la/react](https://demo-v1.captcha.la/react/) — runnable demo with all four product modes. Source: [Captcha-La/react-example](https://github.com/Captcha-La/react-example). ::: ## Install ```bash npm install @captcha-la/react # or yarn add @captcha-la/react # or pnpm add @captcha-la/react ``` Peer dependencies: `react@^17 || ^18 || ^19`, `react-dom@^17 || ^18 || ^19`. ## Quick start ### Component ```tsx import { Captchala } from '@captcha-la/react' function App() { return ( console.log('pass_token:', result.token)} onError={(err) => console.error(err)} /> ) } ``` ### Hook ```tsx import { useCaptchala } from '@captcha-la/react' function LoginForm() { const { ready, verify } = useCaptchala({ appKey: 'your-app-key', product: 'bind', action: 'login', }) async function handleSubmit(e) { e.preventDefault() const result = await verify() // result.token → submit with the form } return (
) } ``` ## Props | Prop | Type | Default | Description | |------|------|---------|-------------| | `appKey` | `string` | *required* | CaptchaLa application key | | `serverToken` | `string` | - | One-time, server-issued challenge token. Required when the app has `server_token_required=true`. | | `product` | `'popup' \| 'float' \| 'embed' \| 'bind'` | `'popup'` | Display mode | | `action` | `string` | `'default'` | Action identifier | | `lang` | `string` | `'zh-CN'` | Language code | | `onSuccess` | `(result) => void` | - | Success callback | | `onError` | `(error) => void` | - | Error callback | | `onClose` | `() => void` | - | Close callback | | `onReady` | `() => void` | - | Ready callback | | `className` | `string` | - | Container class name | | `style` | `CSSProperties` | - | Container inline styles | ## Methods (via `ref`) ```tsx import { useRef } from 'react' import { Captchala, type CaptchalaRef } from '@captcha-la/react' function App() { const ref = useRef(null) return ( <> ) } ``` | Method | Description | |--------|-------------| | `verify()` | Trigger verification | | `reset()` | Reset CAPTCHA state | | `destroy()` | Destroy the instance | | `bindTo(selector)` | Bind to element (for `bind` mode) | | `setLang(lang)` | Switch language in place | ## Production: `serverToken` mode ```tsx import { useState, useEffect } from 'react' import { Captchala } from '@captcha-la/react' function LoginPage() { const [token, setToken] = useState() useEffect(() => { fetch('/api/captcha-token') // your own backend .then((r) => r.json()) .then((d) => setToken(d.server_token)) }, []) if (!token) return
Loading…
return ( console.log('pass_token:', r.token)} /> ) } ``` See [API Reference](/api-reference) for the full backend contract. ## Links - [npm](https://www.npmjs.com/package/@captcha-la/react) · [GitHub](https://github.com/Captcha-La/react) · [example](https://github.com/Captcha-La/react-example) - [Web SDK overview](/web-sdk) · [API Reference](/api-reference)