--- title: React SDK --- # React SDK Component + hook React chính thức cho CaptchaLa CAPTCHA — phát hành dưới dạng [`@captcha-la/react`](https://www.npmjs.com/package/@captcha-la/react). ## Demo trực tiếp ::: tip 📦 [demo-v1.captcha.la/react](https://demo-v1.captcha.la/react/) — demo chạy được với cả bốn chế độ sản phẩm. Mã nguồn: [Captcha-La/react-example](https://github.com/Captcha-La/react-example). ::: ## Cài đặt ```bash npm install @captcha-la/react # hoặc yarn add @captcha-la/react # hoặc pnpm add @captcha-la/react ``` Peer dependencies: `react@^17 || ^18 || ^19`, `react-dom@^17 || ^18 || ^19`. ## Bắt đầu nhanh ### 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 → gửi kèm form } return (
) } ``` ## Props | Prop | Kiểu | Mặc định | Mô tả | |------|------|---------|-------------| | `appKey` | `string` | *bắt buộc* | Khóa ứng dụng CaptchaLa | | `serverToken` | `string` | - | Token thử thách do máy chủ phát hành, dùng một lần. Bắt buộc khi ứng dụng có `server_token_required=true`. | | `product` | `'popup' \| 'float' \| 'embed' \| 'bind'` | `'popup'` | Chế độ hiển thị | | `action` | `string` | `'default'` | Định danh hành động | | `lang` | `string` | `'zh-CN'` | Mã ngôn ngữ | | `onSuccess` | `(result) => void` | - | Callback khi thành công | | `onError` | `(error) => void` | - | Callback khi lỗi | | `onClose` | `() => void` | - | Callback khi đóng | | `onReady` | `() => void` | - | Callback khi sẵn sàng | | `className` | `string` | - | Tên class của container | | `style` | `CSSProperties` | - | Inline style của container | ## Phương thức (qua `ref`) ```tsx import { useRef } from 'react' import { Captchala, type CaptchalaRef } from '@captcha-la/react' function App() { const ref = useRef(null) return ( <> ) } ``` | Phương thức | Mô tả | |--------|-------------| | `verify()` | Kích hoạt xác minh | | `reset()` | Đặt lại trạng thái CAPTCHA | | `destroy()` | Hủy instance | | `bindTo(selector)` | Gắn vào phần tử (cho chế độ `bind`) | | `setLang(lang)` | Đổi ngôn ngữ tại chỗ | ## Production: chế độ `serverToken` ```tsx import { useState, useEffect } from 'react' import { Captchala } from '@captcha-la/react' function LoginPage() { const [token, setToken] = useState() useEffect(() => { fetch('/api/captcha-token') // backend của bạn .then((r) => r.json()) .then((d) => setToken(d.server_token)) }, []) if (!token) return
Loading…
return ( console.log('pass_token:', r.token)} /> ) } ``` Xem [Tài liệu API](/vi/api-reference) để biết toàn bộ hợp đồng phía backend. ## Liên kết - [npm](https://www.npmjs.com/package/@captcha-la/react) · [GitHub](https://github.com/Captcha-La/react) · [ví dụ](https://github.com/Captcha-La/react-example) - [Tổng quan Web SDK](/vi/web-sdk) · [Tài liệu API](/vi/api-reference)