React+TS前台项目实战(二十四)-- 全局常用绘制组件Qrcode封装

news/2024/7/8 2:10:45 标签: react.js, typescript, 前端, 前端框架

文章目录

  • 前言
  • Qrcode组件
    • 1. 功能分析
    • 2. 代码+详细注释
    • 3. 使用方式
    • 4. 效果展示(pc端 / 移动端)
  • 总结


前言

今天要封装的Qrcode 组件,是通过传入的信息,绘制在二维码上,可用于很多场景,如区块链项目中的区块显示交易地址时就可以用到。

Qrcode组件

1. 功能分析

(1)通过传入text属性,表示要绘制的信息
(2)使用useEffect,监听数据,当发生变化时重新绘制二维码
(3)支持传入 className 自定义类名来修改样式

2. 代码+详细注释

// @/components/Qrcode/index.tsx
import { useEffect, useRef, FC } from "react";
import QRCode from "qrcode";
import { ReactComponent as QrCodeIcon } from "./assets/qrcode.svg";
import { QrcodeContainer } from "./styled";
import classNames from "classnames";

// 组件的属性类型
type Props = {
  text: string; // 要绘制的二维码内容
  className?: string; // 自定义的类名
};

const Qrcode: FC<Props> = ({ text, className }) => {
  const qrRef = useRef<HTMLCanvasElement | null>(null);
  useEffect(() => {
    // 获取canvas元素ref
    const cvs = qrRef.current;
    // 如果没有 canvas 元素的引用,则直接返回
    if (!cvs) return;
    // 调用 QRCode.toCanvas 方法,将text绘制到canvas上
    QRCode.toCanvas(
      cvs,
      text,
      {
        margin: 5, // 设置二维码周围的边距
        errorCorrectionLevel: "H", // 设置二维码的容错级别
        width: 144, // 设置二维码的宽度
      },
      (err) => {
        if (err) {
          console.error(err);
        }
      }
    );
  }, [qrRef, text]); // 监听qrRef和text,当发生变化时重新绘制二维码

  return (
    <QrcodeContainer className={classNames(className)}>
      <label>
        <QrCodeIcon />
      </label>
      <canvas ref={qrRef} className={classNames("qrcode")} />
    </QrcodeContainer>
  );
};

export default Qrcode;
------------------------------------------------------------------------------
// @/components/Qrcode/styled.tsx
import styled from "styled-components";
import variables from "@/styles/variables.module.scss";
export const QrcodeContainer = styled.div`
  width: 100%;
  position: relative;
  cursor: pointer;
  label {
    display: flex;
    align-items: center;
    cursor: pointer;
  }
  .qrcode {
    top: calc(100% + 10px);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  }
  &::after {
    top: calc(100% - 15px);
    content: "";
    width: 5px;
    height: 5px;
    border: 10px solid transparent;
    border-bottom: 10px solid #fff;
    filter: drop-shadow(0 -5px 5px rgb(0, 0, 0, 0.1));
  }
  .qrcode,
  &::after {
    display: none;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }
  &:hover,
  &:focus-within {
    .qrcode,
    &::after {
      display: block;
    }
  }
  @media (max-width: ${variables.mobileBreakPoint}) {
    .qrcode,
    &::after {
      left: 0;
    }
  }
`;

3. 使用方式

// 引入组件
import Qrcode from "@/components/Qrcode";
// 使用
const address = "http://test-address?block=XXXX"
<Qrcode text={ address } />

4. 效果展示(pc端 / 移动端)

在这里插入图片描述
在这里插入图片描述


总结

下一篇讲【全局常用组件Echarts封装】。关注本栏目,将实时更新。


http://www.niftyadmin.cn/n/5535821.html

相关文章

一台TrinityCore服务器客户端连接网速慢(未解决)

在FreeBSD开bhyve安装Ubuntu&#xff0c;然后安装了TrinityCore服务器&#xff0c;在只是经过一层NAT&#xff0c;两边都是局域网的情况下&#xff0c;连接速度竟然很慢&#xff0c;慢到600ms。 服务器安装见&#xff1a;尝试在FreeBSD 的jail、bhyve里安装TrinityCore-CSDN博…

C语言实现地铁线路管理系统 含源码 福利

基本功能 线路信息输入 用户能够输入地铁线路编号和站点信息。例如&#xff0c;第一行输入 “9 35” 表示地铁9号线共有35个站点&#xff0c;站点编号从1至35。第二行输入35个站点名称“songjiangnanzhan zuibaichi songjiangtiyuzhongxin songjiangxincheng songjiangdaxuec…

[Go 微服务] Kratos 验证码业务

文章目录 1.环境准备2.验证码服务2.1 kratos 初始化验证码服务项目2.2 使用 Protobuf 定义验证码生成接口2.3 业务逻辑代码实现 1.环境准备 protoc和protoc-gen-go插件安装和kratos工具安装 protoc下载 下载二进制文件&#xff1a;https://github.com/protocolbuffers/protobu…

将List切割为多个指定长度的多个List

参考: https://blog.csdn.net/baidu_41480640/article/details/122507018https://blog.csdn.net/H1767410/article/details/138333350https://blog.51cto.com/u_16213352/7632003https://blog.csdn.net/2301_82243396/article/details/137900249 手写1 private List<List&l…

偏微分方程笔记(驻定与非驻定问题)

椭圆方程可以看成抛物方程 t → ∞ t\rightarrow\infty t→∞的情况。 抛物&#xff1a; 双曲&#xff1a;

CocoaPodsCmake

https://juejin.cn/post/7257048145233838141?searchId20240531171431E5868B41DC7B7016CCBA https://guides.cocoapods.org CocoaPods CocoaPods的作用 帮助程序员通过命令管理第三方库及更新&#xff0c;以达到扩展项目的目的。 CocoaPods的使用 在已有的工程目录下新增…

DPDK概述

文章目录 1. DPDK概述1.1 DPDK 内存管理Mbuf单帧结构:1.2 DPDK内核驱动 igb_uio驱动1.3 DPDK源码下载方式1.4 pktgen源码下载方式1.5 DPDK相关名词解释 1. DPDK概述 Intel DPDK全称Intel Data Plane Development Kit&#xff0c;是Intel提供的数据平面开发工具集&#xff0c;为…

[吃瓜教程]南瓜书第5章神经网络

1.M-P神经元 M-P神经元&#xff0c;全称为McCulloch-Pitts神经元&#xff0c;是一种数学模型&#xff0c;用于模拟生物神经元的功能。这个模型是由Warren McCulloch和Walter Pitts在1943年提出的。它是人工智能和计算神经科学领域中非常重要的早期模型。 M-P神经元接收n个输入…