当前位置: 首页 > 工具软件 > Arale > 使用案例 >

【arale-qrcode二次封装食用】前端生成二维码无需联网

戴鸿羲
2023-12-01

封装一个二维码的弹窗

import React, { useState, useEffect, useRef } from 'react';
import { Modal } from 'antd';
import {
    CopyOutlined,
    CheckCircleOutlined,
    CloseCircleOutlined,
    CloseOutlined,
    CheckOutlined,
    QrcodeOutlined,
} from '@ant-design/icons';

import AraleQRCode from 'arale-qrcode';
let refreshTimer = null;
export default ({ QrCode, isQrcodeModalVisible, setIsQrcodeModalVisible }) => {
    const svg = useRef(null);

    useEffect(() => {
        clearTimeout(refreshTimer);

        refreshTimer = setTimeout(() => {
            autoSearch();
        }, 100);
    }, [QrCode]);
    const autoSearch = () => {
        const result = new AraleQRCode({
            render: 'svg' /*  生成的类型 'svg' or 'table dom元素类型 */,
            text: JSON.stringify(QrCode) /* 二维码的链接*/,
            size: 200 /* 二维码的大小 */,
        });
        if (svg.current) {
            svg.current.appendChild(result);
            console.log('webCode', QrCode);
        }
    };

    const handleOk = () => {
        setIsQrcodeModalVisible(false);
    };

    const handleCancel = () => {
        setIsQrcodeModalVisible(false);
    };

    return (
        <div>
            <Modal
                title="扫描二维码获取机器码"
                visible={isQrcodeModalVisible}
                onOk={handleOk}
                onCancel={handleCancel}
                width={273}
                footer={false}
            >
                <div>
                    <div style={{ marginLeft: 15 }} ref={svg}></div>
                </div>
            </Modal>
        </div>
    );
};

食用

   <QrCodeModal
          QrCode={QrCode}
          isQrcodeModalVisible={isQrcodeModalVisible}
          setIsQrcodeModalVisible={setIsQrcodeModalVisible}
        ></QrCodeModal>
 类似资料: