npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

google-react-recaptcha-v2

v1.0.2

Published

React component for Google reCAPTCHA

Readme

🛡️ React ReCaptchaV2

A lightweight and developer-friendly React component for rendering an invisible Google reCAPTCHA v2 widget. It supports custom callbacks, localization, badge positioning, and can be triggered programmatically using ref.

✅ Supports TypeScript
✅ Works in development and production environments
✅ Easily integrates with forms or async flows
✅ Minimal and clean API


📦 Installation

npm install google-react-recaptcha-v2
# or
yarn add google-react-recaptcha-v2
# or
yarn add google-react-recaptcha-v2

🚀 Getting Started

1. Import the Component

import { ReCaptchaV2, ReCaptchaV2Ref } from "google-react-recaptcha-v2";

2. Add the Component to Your Form

import { useRef } from "react";
import { ReCaptchaV2, ReCaptchaV2Ref } from "google-react-recaptcha-v2";

export default function MyForm() {
  const recaptchaRef = useRef<ReCaptchaV2Ref>(null);

  const handleSubmit = async (e) => {
    e.preventDefault();
    const token = await recaptchaRef.current?.execute();
    // Puedes usar el token aquí
    console.log("reCAPTCHA token:", token);
  };

  return (
    <>
      <form onSubmit={handleSubmit}>
        <button type="submit">Submit</button>
      </form>

      <ReCaptchaV2
        ref={recaptchaRef}
        siteKey="YOUR_PRODUCTION_SITE_KEY"
        // onSuccess es opcional
      />
    </>
  );
}

🧠 Props

ReCaptchaV2Props

| Prop | Type | Required | Default | Description | | ----------- | ------------------------------------------- | -------- | --------------- | ------------------------------------------------------------- | | siteKey | string | ✅ | – | Your production Google reCAPTCHA v2 site key. | | onSuccess | (token: string) => void | ❌ | – | Callback opcional cuando reCAPTCHA se resuelve exitosamente. | | onError | () => void | ❌ | – | Callback when reCAPTCHA fails to render or complete. | | onExpired | () => void | ❌ | – | Called when the token expires before verification. | | badge | "bottomright" \| "bottomleft" \| "inline" | ❌ | "bottomright" | Position of the reCAPTCHA badge on screen. | | hl | string | ❌ | – | Language code (e.g., "en", "es") for localization. | | isDevelop | boolean | ❌ | false | Uses Google's default test key if true ("6LeIxAcTAAAA..."). | | size | "invisible" \| "normal" \| "compact" | ❌ | invisible | Size of the recaptcha |


🧪 Ref Methods

ReCaptchaV2Ref

| Method | Type | Description | | --------- | ----------------------- | ------------------------------------------------------ | | execute | () => Promise<string> | Ejecuta el reto invisible y retorna el token generado. | | reset | () => void | Resetea el widget y limpia el token actual. |


📌 Examples

🌐 Con idioma personalizado y uso de execute

const recaptchaRef = useRef<ReCaptchaV2Ref>(null);

const handleClick = async () => {
  const token = await recaptchaRef.current?.execute();
  console.log("Token:", token);
};

<ReCaptchaV2 ref={recaptchaRef} siteKey="YOUR_SITE_KEY" hl="es" />;

🧪 Modo desarrollo

const recaptchaRef = useRef<ReCaptchaV2Ref>(null);

const handleClick = async () => {
  const token = await recaptchaRef.current?.execute();
  console.log("Test token:", token);
};

<ReCaptchaV2 isDevelop ref={recaptchaRef} siteKey="will-be-ignored" />;

🧭 Posición personalizada del badge

const recaptchaRef = useRef<ReCaptchaV2Ref>(null);

const handleClick = async () => {
  const token = await recaptchaRef.current?.execute();
  console.log("Token:", token);
};

<ReCaptchaV2 badge="bottomleft" ref={recaptchaRef} siteKey="YOUR_SITE_KEY" />;

🧰 Tips

  • The reCAPTCHA will not trigger until you call execute() manually.
  • Invisible reCAPTCHA es ideal para validaciones no intrusivas antes de enviar acciones sensibles (ej: formularios, comentarios, eliminar).
  • Si necesitas retos visibles, cambia el prop size a "normal" o "compact".

🏗️ Arquitectura limpia

La librería está estructurada siguiendo Clean Architecture:

  • domain: Interfaces y tipos principales.
  • application: Hook/caso de uso para el ciclo de vida y callbacks.
  • infrastructure: Servicio para cargar y renderizar el widget/script.
  • presentation: Componente React que solo orquesta y delega.

Esto permite fácil mantenimiento, testeo y extensión.


🧪 Test Key for Local Development

Use this key when testing locally (no need to register it):

siteKey = "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI";

📜 License

MIT © Andrés Felipe Hernández Aldana


💡 Credits

Built using: