kybarg / react-qr-scanner

React component for reading QR codes using PC web camera or mobile phone camera.
MIT License
124 stars 40 forks source link

Issues with Next.js #38

Open wesbos opened 3 years ago

wesbos commented 3 years ago

Hello - I think this fork does SSR, right?

When I import it like so:

import QrReader from 'react-qr-scanner';

I get this issue: SyntaxError: Cannot use import statement outside a module pointing to > 1 | module.exports = require("react-qr-scanner");

I looked through the source and can't seem to find why it would think it's cjs. Any ideas?

It does work if I use next.js dynamic import, but then that that annoy as it re-imports it when it re-renders.

const QrReader = dynamic(() => import('react-qr-scanner'));
wesbos commented 3 years ago

I think this has to do with the Module not being shipped as CJS, or with a .MJS extension?

csellis commented 3 years ago

Did you make any progress @wesbos ? I'm looking at this same use case

wesbos commented 3 years ago

Yes, I used an alternative lazy import package for React.I forget the name, but it was the big one...

theabdulmateen commented 3 years ago

Yes, I used an alternative lazy import package for React.I forget the name, but it was the big one...

By this do you mean wait till the page is delivered from nextjs and then load the QrReader? In that case you would only need to something of this sort right?

useEffect(() => {
  setHasRendered(true)
}, [])
...
...

<div>
{hasRendered && (
    <QrReader
      ...
    />
  )}
</div>
ninest commented 3 years ago

Apart from the above solution, this works for me

<div>
  {typeof window !== "undefined" && (
    <>
      <QRReader
        onScan={onScan}
        onError={onError}
      ></QRReader>
    </>
  )}
</div>