--- title: SDK React --- # SDK React Composant et hook React officiels pour le CAPTCHA CaptchaLa — publiés sous [`@captcha-la/react`](https://www.npmjs.com/package/@captcha-la/react). ## Démo en ligne ::: tip 📦 [demo-v1.captcha.la/react](https://demo-v1.captcha.la/react/) — démo exécutable avec les quatre modes produit. Source : [Captcha-La/react-example](https://github.com/Captcha-La/react-example). ::: ## Installation ```bash npm install @captcha-la/react # or yarn add @captcha-la/react # or pnpm add @captcha-la/react ``` Dépendances pairs : `react@^17 || ^18 || ^19`, `react-dom@^17 || ^18 || ^19`. ## Démarrage rapide ### Composant ```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 | Valeur par défaut | Description | |------|------|---------|-------------| | `appKey` | `string` | *requis* | Clé d'application CaptchaLa | | `serverToken` | `string` | - | Token de challenge émis par le serveur, à usage unique. Requis lorsque l'application a `server_token_required=true`. | | `product` | `'popup' \| 'float' \| 'embed' \| 'bind'` | `'popup'` | Mode d'affichage | | `action` | `string` | `'default'` | Identifiant de l'action | | `lang` | `string` | `'zh-CN'` | Code de langue | | `onSuccess` | `(result) => void` | - | Callback de succès | | `onError` | `(error) => void` | - | Callback d'erreur | | `onClose` | `() => void` | - | Callback de fermeture | | `onReady` | `() => void` | - | Callback de disponibilité | | `className` | `string` | - | Nom de classe du conteneur | | `style` | `CSSProperties` | - | Styles inline du conteneur | ## Méthodes (via `ref`) ```tsx import { useRef } from 'react' import { Captchala, type CaptchalaRef } from '@captcha-la/react' function App() { const ref = useRef(null) return ( <> ) } ``` | Méthode | Description | |--------|-------------| | `verify()` | Déclenche la vérification | | `reset()` | Réinitialise l'état du CAPTCHA | | `destroy()` | Détruit l'instance | | `bindTo(selector)` | Lie à un élément (pour le mode `bind`) | | `setLang(lang)` | Change la langue à la volée | ## Production : mode `serverToken` ```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)} /> ) } ``` Consultez la [Référence de l'API](/fr/api-reference) pour le contrat backend complet. ## Liens - [npm](https://www.npmjs.com/package/@captcha-la/react) · [GitHub](https://github.com/Captcha-La/react) · [exemple](https://github.com/Captcha-La/react-example) - [Vue d'ensemble du SDK Web](/fr/web-sdk) · [Référence de l'API](/fr/api-reference)