美文网首页
react+typescript 封装条形码组件Barcode

react+typescript 封装条形码组件Barcode

作者: chenjieyi | 来源:发表于2022-07-04 12:07 被阅读0次

Barcode.tsx 组件代码

import React, { useRef, useEffect } from 'react';

const JsBarcode = require('jsbarcode');

interface BarcodeProps {
  value?: string;
  height?: number;
}

const Index: React.FC<BarcodeProps> = ({ value, height = 40 }) => {
  const ref = useRef<SVGSVGElement>(null);

  useEffect(() => {
    if (value && ref.current)
      JsBarcode(ref.current, value, {
        height,
      }).render();
  }, [value]);

  return <svg ref={ref} />;
};

export default Index;

使用Barcode组件

<Barcode value={"test"} />

效果如下图


条形码效果图

相关文章

网友评论

      本文标题:react+typescript 封装条形码组件Barcode

      本文链接:https://www.haomeiwen.com/subject/pvsdbrtx.html