--- title: React SDK --- # React SDK Komponen + hook React rasmi untuk CAPTCHA CaptchaLa — diterbitkan sebagai [`@captcha-la/react`](https://www.npmjs.com/package/@captcha-la/react). ## Demo langsung ::: tip 📦 [demo-v1.captcha.la/react](https://demo-v1.captcha.la/react/) — demo boleh dijalankan dengan keempat-empat mod produk. Sumber: [Captcha-La/react-example](https://github.com/Captcha-La/react-example). ::: ## Pemasangan ```bash npm install @captcha-la/react # atau yarn add @captcha-la/react # atau pnpm add @captcha-la/react ``` Kebergantungan rakan: `react@^17 || ^18 || ^19`, `react-dom@^17 || ^18 || ^19`. ## Mula pantas ### Komponen ```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 → hantar bersama borang } return (
) } ``` ## Props | Prop | Jenis | Lalai | Penerangan | |------|------|---------|-------------| | `appKey` | `string` | *diperlukan* | Kunci aplikasi CaptchaLa | | `serverToken` | `string` | - | Token cabaran sekali guna yang dikeluarkan pelayan. Diperlukan apabila aplikasi mempunyai `server_token_required=true`. | | `product` | `'popup' \| 'float' \| 'embed' \| 'bind'` | `'popup'` | Mod paparan | | `action` | `string` | `'default'` | Pengecam tindakan | | `lang` | `string` | `'zh-CN'` | Kod bahasa | | `onSuccess` | `(result) => void` | - | Panggilan balik kejayaan | | `onError` | `(error) => void` | - | Panggilan balik ralat | | `onClose` | `() => void` | - | Panggilan balik tutup | | `onReady` | `() => void` | - | Panggilan balik sedia | | `className` | `string` | - | Nama kelas bekas | | `style` | `CSSProperties` | - | Gaya sebaris bekas | ## Kaedah (melalui `ref`) ```tsx import { useRef } from 'react' import { Captchala, type CaptchalaRef } from '@captcha-la/react' function App() { const ref = useRef(null) return ( <> ) } ``` | Kaedah | Penerangan | |--------|-------------| | `verify()` | Cetus pengesahan | | `reset()` | Set semula keadaan CAPTCHA | | `destroy()` | Musnahkan instance | | `bindTo(selector)` | Ikat ke elemen (untuk mod `bind`) | | `setLang(lang)` | Tukar bahasa di tempat | ## Pengeluaran: mod `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 anda sendiri .then((r) => r.json()) .then((d) => setToken(d.server_token)) }, []) if (!token) return
Loading…
return ( console.log('pass_token:', r.token)} /> ) } ``` Lihat [Rujukan API](/ms/api-reference) untuk kontrak backend yang lengkap. ## Pautan - [npm](https://www.npmjs.com/package/@captcha-la/react) · [GitHub](https://github.com/Captcha-La/react) · [contoh](https://github.com/Captcha-La/react-example) - [Gambaran Web SDK](/ms/web-sdk) · [Rujukan API](/ms/api-reference)