Socket
Socket
Sign inDemoInstall

react-captcha-code

Package Overview
Dependencies
85
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-captcha-code

基于React和canvas的验证码


Version published
Weekly downloads
156
increased by11.43%
Maintainers
1
Install size
6.69 MB
Created
Weekly downloads
 

Readme

Source

react-captcha-code

基于 Reactcanvas 的一个验证码组件

安装

npm install react-captcha-code --save

需要用户自己安装的依赖

同级依赖:

依赖名称版本
react^16.13.1
react-dom^16.13.1

APIS

名称类型是否必填默认值描述
heightnumber40验证码的高度
widthnumber100验证码的宽度
bgColorstring#DFF0D8背景颜色
charNumnumber4字符个数
fontSizenumber25字体大小
codestring要展示的验证码(受控)
onChange(captcha: string) => void点击验证码的回调函数, 用来传递验证码(会在页面初始加载和点击验证码时调用)
onClick() => void点击验证码的回调函数
onRef(ref: any) => void在验证码组件初次挂载时调用,返回 canvas DOM(可主动调用 canvas.click() 来刷新验证码) 不推荐使用,推荐使用下面的 ref 获取刷新接口
ref-推荐使用 ref 获取刷新接口canvasRef.current.refresh() 组件内部通过过useImperativeHandle 暴露 refresh 接口
classNamestring样式名

基本用法

代码示例

import React, { useCallback, useRef } from 'react';
import Captcha from 'react-captcha-code';

export const Basic = () => {
  const handleChange = useCallback((captcha) => {
    console.log('captcha:', captcha);
  }, []);

  const captchaRef = useRef<HTMLCanvasElement>();

  const handleClick = () => {
    // 刷新验证码
    (captchaRef as any).current.refresh();
  };

  return (
    <>
      <Captcha ref={captchaRef} charNum={6} onChange={handleChange} />
      <div>
        <button onClick={handleClick}>更换验证码</button>
      </div>
    </>
  );
};

效果

Edit zen-paper-9yish

更新日志

[1.0.5] - 2021-01-07

Features

  • 暴露refresh接口来刷新验证码

Fix

  • onRef 改为可选

[1.0.6] - 2021-02-03

Features

  • 验证码组件支持受控

Keywords

FAQs

Last updated on 24 Feb 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc