React SDK
Official React component + hook for CaptchaLa CAPTCHA — published as @captcha-la/react.
Live demo
📦
demo-v1.captcha.la/react — runnable demo with all four product modes. Source: Captcha-La/react-example.
Install
bash
npm install @captcha-la/react
# or
yarn add @captcha-la/react
# or
pnpm add @captcha-la/reactPeer dependencies: react@^17 || ^18 || ^19, react-dom@^17 || ^18 || ^19.
Quick start
Component
tsx
import { Captchala } from '@captcha-la/react'
function App() {
return (
<Captchala
appKey="your-app-key"
product="popup"
onSuccess={(result) => 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 (
<form onSubmit={handleSubmit}>
<button type="submit" disabled={!ready}>Login</button>
</form>
)
}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<CaptchalaRef>(null)
return (
<>
<Captchala ref={ref} appKey="your-app-key" />
<button onClick={() => ref.current?.verify()}>Verify</button>
</>
)
}| 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<string>()
useEffect(() => {
fetch('/api/captcha-token') // your own backend
.then((r) => r.json())
.then((d) => setToken(d.server_token))
}, [])
if (!token) return <div>Loading…</div>
return (
<Captchala
serverToken={token}
appKey="pk_your_public_app_key"
action="login"
onSuccess={(r) => console.log('pass_token:', r.token)}
/>
)
}See API Reference for the full backend contract.