0

Hi I was trying to use react-qr-reader in next js but having the problem

Server Error
ReferenceError: Blob is not defined

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Source
external%20%22react-qr-reader%22 (1:0) @ Object.react-qr-reader

> 1 | module.exports = require("react-qr-reader");
Call Stack
__webpack_require__
webpack\bootstrap (21:0)

How can I fix it?

Robbi
  • 71
  • 4
  • 11

3 Answers3

3

This works for me:

const QrScan = dynamic(() => import('react-qr-reader'), { ssr: false })
juliomalves
  • 21,997
  • 12
  • 75
  • 81
1

The official docs says Server side rendering won't work for react-qr-reader. So you need to do is to avoid applying react-qr-reader in server-side. You can use the dynamic to solve the problem. You can also reference from the solution 2 of this solution to get some example code.

bcjohn
  • 1,764
  • 8
  • 23
1

Great work guys, this works for me

import { useState } from "react";
import dynamic from "next/dynamic";
const QrReader = dynamic(() => import("react-qr-reader"), { ssr: false });


export default function ScanPage() {
const [state, setState] = useState("");

return (
<>
<div>{state}</div>
<QrReader delay={100}
onError={(err) => setState(err)}
onScan={(data) => setState(data)}
style={{ width: "95vw"}}
/>
</>
);}