QrCode.jsx 572B

12345678910111213141516171819202122232425262728
  1. import React from 'react';
  2. export default (props) => {
  3. const { text, width = 200, height = 200 } = props;
  4. const ref = React.useRef();
  5. const qrRef = React.useRef();
  6. React.useEffect(() => {
  7. if (text) {
  8. if (qrRef.current) {
  9. qrRef.current.clear();
  10. qrRef.current.makeCode(text);
  11. } else {
  12. qrRef.current = new QRCode(ref.current, {
  13. text,
  14. width,
  15. height,
  16. correctLevel: QRCode.CorrectLevel.Q
  17. });
  18. }
  19. }
  20. }, [text, width, height]);
  21. return (
  22. <div ref={ref}></div>
  23. )
  24. }