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

Open
Jervx opened this issue Feb 21, 2024 · 3 comments
Open

Nextjs Only 1 Instance problem #200

Jervx opened this issue Feb 21, 2024 · 3 comments

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants