index.tsx 921 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { Popover } from "antd"
  2. import React, { useEffect, useRef, useState } from "react"
  3. import QRCode from 'qrcodejs2'
  4. interface Props {
  5. url: string
  6. }
  7. const QrCode: React.FC<Props> = ({ url }) => {
  8. /********************************/
  9. const ref = useRef(null)
  10. const [visible, setVisible] = useState<boolean>(false)
  11. const [qr, setQr] = useState<any>()
  12. /********************************/
  13. useEffect(() => {
  14. if (ref.current && url && visible && !qr) {
  15. let a = new QRCode(ref.current, {
  16. text: url,
  17. width: 150,
  18. height: 150
  19. })
  20. setQr(a)
  21. }
  22. }, [ref, visible])
  23. return <Popover
  24. content={<div ref={ref}></div>}
  25. onVisibleChange={(e) => {
  26. setVisible(e)
  27. }}
  28. >
  29. <a style={{ fontSize: 12 }}>预览</a>
  30. </Popover>
  31. }
  32. export default React.memo(QrCode)