Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextjs Only 1 Instance problem #200

Closed
Jervx opened this issue Feb 21, 2024 · 4 comments
Closed

Nextjs Only 1 Instance problem #200

Jervx opened this issue Feb 21, 2024 · 4 comments
Labels
reviewed Temporary label to review all open and closed PR

Comments

@Jervx
Copy link

Jervx commented Feb 21, 2024

I have this code, then I called it 2x somewhere

"use client"
import React, { useEffect, useRef } from "react";
import QRCodeStyling from "qr-code-styling";

import { cn } from "@/lib/utils";

type Props = {
    className?: string;
    value: string;
};

const qrCode = new QRCodeStyling({
    width: 260,
    height: 260,
    type: "svg",
    backgroundOptions : {
        color : "transparent"
    }, 
    dotsOptions: {
        color: "#4267b2",
        type: "rounded",
    },
});

const QRCode = ({ className, value }: Props) => {
    const ref = useRef<HTMLDivElement>(null);

    useEffect(()=>{
        if (!ref || !ref.current) return;
        qrCode.update({
            data: value
          });
    },[value])

    useEffect(() => {
        if (!ref || !ref.current) return;
        qrCode.append(ref.current);
    }, []);

    return (
        <div className={cn("rounded-xl overflow-clip", className)} ref={ref} />
    );
};

export default QRCode;

I called it 2x here

           <div> 
                <QRCode value={'yes'}/>
                <QRCode value={'a different one'}/>
            </div>

it only shows the last one
image

when I removed the last <QRCode value={'a different one'}/> this is the result

image

How can I prevent this from happening?

@Jervx
Copy link
Author

Jervx commented Feb 21, 2024

I also tried it on the sample code which is also happens thesame

image

@vezhdelit
Copy link

I have the same issue. No solution found yet

@korizyx
Copy link

korizyx commented Aug 9, 2024

I found a way to deal with it. Instead of svg, we'll use canvas. By some chance the library can't separate the different information for each svg, so they'll have the same value, and by switching to rendering with canvas we won't have this problem. In addition, I've also allocated to instantiate the qrcode class inside the component function, thus creating a separate instance for each qrcode, avoiding replicating the qrcode values. My code below:

src/app/page.tsx

"use client";

import * as S from './styles';
import QRCodeStyling from 'qr-code-styling';

interface Props {
  value: string;
}

const QRCode = ({ value }: Props) => {
  const qrCode = new QRCodeStyling({
    width: 500,
    height: 500,
    type: 'canvas',
    backgroundOptions: {
      color: 'transparent',
    },
    dotsOptions: {
      color: '#4267b2',
      type: 'rounded',
    },
    data: value,
  });

  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    if (!ref || !ref.current) return;
    qrCode.update({
      data: value,
    });
  }, [value]);

  useEffect(() => {
    if (!ref || !ref.current) return;
    qrCode.append(ref.current);
  }, []);

  return <S.Qrcode ref={ref} />;
}; 

export default function Page() {
  return (
    <S.Container>
      hi
      <QRCode value={'01011010100101'} />
      other
      <QRCode value={'awdawddawdawd'} />
    </S.Container>
  );
};

image

@kozakdenys
Copy link
Owner

Hi @Jervx @vezhdelit @korizyx the issue with 2 qr-codes on the same page was fixed in version v1.7.1 and above.
Also, I have added an example of usage of this package on the Next.js (server component and client)

@kozakdenys kozakdenys added the reviewed Temporary label to review all open and closed PR label Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reviewed Temporary label to review all open and closed PR
Projects
None yet
Development

No branches or pull requests

4 participants