react-qr-scanner icon indicating copy to clipboard operation
react-qr-scanner copied to clipboard

Issues with Next.js

Open wesbos opened this issue 4 years ago • 5 comments

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 avatar Jan 01 '21 22:01 wesbos

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

wesbos avatar Jan 06 '21 01:01 wesbos

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

csellis avatar Mar 19 '21 06:03 csellis

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

wesbos avatar Mar 24 '21 20:03 wesbos

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>

theabdulmateen avatar Apr 22 '21 09:04 theabdulmateen

Apart from the above solution, this works for me

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

ninest avatar May 17 '21 08:05 ninest