react-modal icon indicating copy to clipboard operation
react-modal copied to clipboard

Default export is not working with typescript

Open testerez opened this issue 6 years ago • 14 comments

Summary:

Using typescript, if I import with import ReactModal from 'react-modal'; then ReactModal is undefined. It usually means that the module has no default export. import * as ReactModal from 'react-modal'; works but does not satisfy the definitions from @types/[email protected]

Steps to reproduce:

test.ts

import ReactModal from 'react-modal';
console.log(ReactModal);

ts-node test.ts

Expected behavior:

Should log a component but logs undefined

testerez avatar Sep 09 '17 04:09 testerez

@testerez we have recently release a patch to fix the imports. Can you check if this happens on latest version?

diasbruno avatar Sep 09 '17 13:09 diasbruno

Tested with version 2.3.2 which seems to be the latests.

testerez avatar Sep 09 '17 14:09 testerez

Is it still failing?

diasbruno avatar Sep 09 '17 14:09 diasbruno

.babelrc#L4 This was recently added. It shouldn't cause any conflict, but you can try to remove it and see if it helps...

diasbruno avatar Sep 09 '17 14:09 diasbruno

I have the same problem. I tried:

.babelrc#L4 This was recently added. It shouldn't cause any conflict, but you can try to remove it and see if it helps...

It dit not help. I am using typescript with the versions: "react": "15.4.2", "types/react": "15.0.38", "react-modal": "2.3.2", "types/react-modal": "2.2.1",

my code looks like this: import ReactModal from 'react-modal';

<ReactModal isOpen={true} contentLabel='Hello world'> <p>Hello World</p> </ReactModal>

The error I get is:

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of CardInputContainer.

When I use: import * as ReactModal from 'react-modal'; I get the compile error:

Error TS2604: JSX element type 'ReactModal' does not have any construct or call signatures.

I hope you can help me with this.

EDIT: It works when I use the same versions: "react-modal": "2.2.1", "types/react-modal": "2.2.1",

MikeMolIS avatar Sep 15 '17 07:09 MikeMolIS

@MikeMolIS Perhaps, the types/react-modal needs to keep sync with new releases (?). I understand how the type provider works, but I'm not familiar with this repo. Can you post the links to this repo?

diasbruno avatar Sep 15 '17 12:09 diasbruno

Here it is: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-modal/index.d.ts But the definitions does not change anything to the output. They just help giving warnings if the typing is incorrect. Be aware that babel does some magic with the imports: import foo from 'foo' means that you import the default export of 'foo'. If foo has no default export foo should be undefined. But Babel add some glue so if foo has no default export you will get *. Typescript is more strict and does not do that. It could explain why import ReactModal from 'react-modal'; works with babel but not with typescript.

testerez avatar Sep 15 '17 12:09 testerez

The confusing babel output.

exports.default = _Modal2.default;
module.exports = exports['default'];

Not using defaults just makes the world a happier place, without a default people can still just rename the var if needed using: import {ReactModal as RModal} from "react-modal" so the default syntax doesn't add anything. (Well you can skip two { } i guess).

So the most comfy fix would be to skip the default. Then it would work for everyone.

Background: exporting 'exports.default' rather than 'exports' #706

Psvensso avatar Oct 02 '17 11:10 Psvensso

Any update on the same ?

Due to usage of babel-plugin-add-module-exports, code spit out is incompatible with webpack 2.2. One way to fix this is defined in this gist.

bogas04 avatar Nov 01 '17 08:11 bogas04

@bogas04 Sounds great. This is still open, they have changed on their side (DefinitelyTyped), but if we can provide the correct way, it would be nicer. Can you PR this?

diasbruno avatar Nov 01 '17 11:11 diasbruno

I was having this issue and got it to work by using the require syntax instead of the import...from i.e. import ReactModal = require('react-modal');

Looking at the PR above, the Typescript tests were updated to use the require syntax as well. Can this be added to the documentation?

not-mike-smith avatar Jan 15 '19 16:01 not-mike-smith

This is still happening #768

Zorig avatar Jul 29 '19 03:07 Zorig

Importing ReactModal with default exports:

import * as ReactModal from 'react-modal';

Still got problems with this image

rgb2hsl avatar Jun 18 '21 15:06 rgb2hsl

@ru-web-designer You should import with import ReactModal form 'react-modal';.

Or...see what this logs:

import * as ReactModal from 'react-modal';
console.log(ReactModal);

diasbruno avatar Jun 19 '21 02:06 diasbruno