New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

use-captcha-react

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-captcha-react

React hook for captcha providers

  • 1.0.0-beta.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

use-captcha-react

A generic wrapper for using captcha scripts on the client side in React applications.

npm version

Description

use-captcha-react is a generic wrapper for using captcha scripts on the client side in React applications. It provides a simple and flexible interface to integrate captcha providers, whether pre-configured by the package or custom-built by developers. Perfect for managing captchas consistently and reusable across React projects.

How to Use

Installation

To install the library, use one of the following commands:

npm i use-captcha-react

Or, if you are using pnpm:

pnpm add use-captcha-react

Usage

Use the useCaptcha hook to initialize and interact with the captcha. Pass the provider (e.g., GoogleReCaptchaV2Provider) and the site key to the hook.

const [ref, { executeAsync, getValue }] = useCaptcha(
  GoogleReCaptchaV2Provider,
  siteKey
);

Create a function to handle the form submission. Use executeAsync, for example, to verify the captcha before processing the form.

function handleSubmit(handler: FormEventHandler) {
  return async (event: FormEvent) => {
    event.preventDefault();

    const token = await executeAsync();

    if (token) {
      handler(event);
    }
  };
}

async function handleSuccessSubmit() {
  const token = getValue();
  /* Handle your form submission process here */
}

Attach the ref to an HTML element to display the captcha widget. Wrap your form logic with the handlers.

return (
  <form onSubmit={handleSubmit(handleSuccessSubmit)}>
    <div ref={ref} />
  </form>
);

Core Concepts

useCaptcha Hook

The useCaptcha hook is the main entry point for using captchas with this library. It takes a provider as input and abstracts its initialization and execution methods, offering a more "plug-and-play" experience.

This hook not only simplifies captcha usage but also exposes the provider instance, allowing developers to directly manipulate it or create custom methods if needed.

Extending Captcha Functionality

The captcha provider can include additional methods if the developer wants to expose more functionality. While the default interface covers the core methods, you can extend it to suit your specific requirements or to leverage unique features of your chosen captcha provider.

Learn more into the create a provider dedicated page!

Note

The useCaptcha hook manages scripts on the page to ensure no duplicates are added. However, the code executed by these scripts is not controlled by the hook. This means that elements, attributes, or variables generated by these scripts may be executed multiple times if the script is removed and added back to the page.

Motivation

The use-captcha-react package was designed to simplify captcha integration with React's modern hook standards. It is inspired by the react-google-recaptcha library created by Hugo Dozois.

In addition to its hook-based approach, the generic nature of this wrapper allows developers to integrate various captcha providers in a flexible and configurable manner, all within the same library.

Created by Gabriel Jesus.

Keywords

FAQs

Package last updated on 02 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc