2.0.1 • Published 3 years ago

google-v3-recaptchas v2.0.1

Weekly downloads
-
License
-
Repository
-
Last release
3 years ago

google-v3-recaptchas

React component for Google reCAPTCHA v3. Please note this package is only for functional based components, if you want to convert it into class based then handle regenerate states accordingly.

Installation

npm install google-v3-recaptchas

Usage

You need to sign up on Google recaptcha console for sitekey of v3 reCaptcha signup.

Please follow the example below.

Code Example:

import {useState} from 'react';
import V3Recaptcha from "google-v3-recaptchas";

const yourComponent = () => {

    // this usestate hook is required for regeneration process, because recaptcha usually expires after 2 minutes. 
    const [regenerate, setRegenerate] = useState(false)

    const handleChange = (token) => {
        // Whenever a new token is generated on ready or regenerated state, then this function will be run automatically.
        // Handle your token state management in this function accordingly
        console.log(token)
    }

    return (
        <V3Recaptcha 
            sitekey="your_site_key"
            action="login"
            verifyToken={handleChange}
            regenerate={regenerate}
            setRegenerate={setRegenerate}
        />
    )
}

export default yourComponent

Component Props

Properties used to customise the rendering:

NameTypeDescription
sitekeystringYour sitekey from google recaptcha console
actionstringyou will define actions here like, login and homepage etc
verifyTokenfuncWhenever a new token is generated on ready or regenerated state, then this function will be run automatically
regeneratebooleanset this to true if you want to regenerate v3 token on any clicks or before form submission
setRegeneratefuncthis will be a second argument of usestate hook, please use it exactly like we show you in example above

Notes

Made with love by Valeed Mehmood