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

Default import? #251

Open
benlieb opened this issue Jan 20, 2024 · 5 comments
Open

Default import? #251

benlieb opened this issue Jan 20, 2024 · 5 comments

Comments

@benlieb
Copy link

benlieb commented Jan 20, 2024

I have to do this for some reason:

import { QRCode } from 'react-qr-code'; // note different from docs
@am-david-jiang
Copy link

Yes, I have the same problem. If I write import QRCode from 'react-qr-code, the website will not show up. Instead I get this error

image

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

Check the render method of QRCodeGenerator.
at createFiberFromTypeAndProps (react-dom.development.js:28439:17)
at createFiberFromElement (react-dom.development.js:28465:15)
at reconcileSingleElement (react-dom.development.js:15750:23)
at reconcileChildFibers2 (react-dom.development.js:15808:35)
at reconcileChildren (react-dom.development.js:19167:28)
at updateHostComponent (react-dom.development.js:19924:3)
at beginWork (react-dom.development.js:21618:14)
at HTMLUnknownElement.callCallback2 (react-dom.development.js:4164:14)
at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:16)
at invokeGuardedCallback (react-dom.development.js:4277:31)

It seems like the export is a object, like the normal export in ES Module, not the default function component QRCode
(btw I use esbuild to bundle my code, the CLI command I use is esbuild --bundle ./src/main.jsx --outfile=build/main.js --sourcemap --loader:.js=jsx --watch --servedir=build for live reloading)

The problem is solved by wrapping QRCode with curly brackets. like import { QRCode } from 'react-qr-code';

@kblestarge
Copy link

I have to do the same. The named export is the only one that works for me too. TypeScript doesn't like it (unfortunately must use @ts-ignore) but it does work:

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { QRCode } from 'react-qr-code';

techcoderx added a commit to aioha-hive/react-ui that referenced this issue Sep 12, 2024
@redneb
Copy link

redneb commented Dec 6, 2024

I ran into this issue too.

Here's a very minimal example to reproduce this:
(Note: To make this example as minimal as possible, I am not using typescript and jsx syntax)

Create the following 2 files in a new directory:

test.js:

import {jsx as _jsx} from "react/jsx-runtime";
import * as ReactDOMServer from "react-dom/server";
import QRCode from "react-qr-code";

const qrcode = _jsx(QRCode, {value: "This is a test"});
console.log(ReactDOMServer.renderToStaticMarkup(qrcode));

package.json:

{
  "type": "module",
  "dependencies": {
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "react-qr-code": "^2.0.15"
  }
}

The test script attempts to render <QRCode value="this is a test" /> in server-side rendering mode, which should produce a string containing the corresponding HTML code.

If you try to run this with node test.js you will get an error. If you remove the line "type": "module" from package.json, then it runs fine. If you want to use "type": "module", then you have to use QRCode.default instead of QRCode.

@redneb
Copy link

redneb commented Dec 6, 2024

My workaround in typescript that allows me to use this package is:

import QRCode_import from "react-qr-code";
const QRCode = (QRCode_import as unknown as {default: typeof QRCode_import}).default;

This avoids the use of @ts-ignore and QRCode is actually typed.

@CanRau
Copy link

CanRau commented Dec 8, 2024

Facing the same.

Might be because type is not set to module https://nodejs.org/api/packages.html#type
and/or because exports is missing https://nodejs.org/api/packages.html#package-entry-points

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

5 participants