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

React.createElement: type is invalid #150

Closed
otakueadwine opened this issue Jan 5, 2018 · 18 comments
Closed

React.createElement: type is invalid #150

otakueadwine opened this issue Jan 5, 2018 · 18 comments

Comments

@otakueadwine
Copy link

Hi! Thank you for creating this plugin!
I'm currently trying to implement it in my project, but I keep getting the following error:
warning.js:33 Warning: React.createElement: 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 AddEditLinkModal.

Perhaps I'm implementing it the wrong way?
I'm using webpack for my assets. I've put "react-image-crop" inside my "vendor" entry.

Inside my react component I've done the following import:
import ReactCrop from "react-image-crop";
And the logic I created:

public state: State = {
	crop: {
		x: 0,
		y: 0,
		aspect: 10 / 4,
		width: 50,
	},
};
constructor(props: Props, state: State) {
	super(props, state);

	this.onCropImageChange = this.onCropImageChange.bind(this);
}

public handleImgSelection(e: any) {
	e.preventDefault();
	const {setImg} = this.props;

	const reader = new FileReader();
	const file = e.target.files[0];

	reader.onloadend = () => {
		setImg(file, reader.result);
	};

	reader.readAsDataURL(file);
}

public onCropImageChange(crop: State["crop"]) {
	this.setState({crop: crop});
}

Inside my render I'm rendering the following ReactCrop:
<ReactCrop crop={this.state.crop} src={imgPreview} onChange={this.onCropImageChange} />

The imgPreview is the reader.result which is saved to my redux store.

@sekoyo
Copy link
Owner

sekoyo commented Jan 9, 2018

Hi,

Typically something you are importing is undefined. Did you figure this out? You shouldn't need to add this to a vendor option if you're referring to build tools like webpack, just the import should do. If below the import you do console.log(ReactCrop) what does it give?

@danielasy
Copy link

Hello, I was getting a similar error when importing with import ReactCrop from 'react-image-crop'. ReactCrop would be set to undefined.

Using import * as ReactCrop from 'react-image-crop' instead seemed to do it.

@sekoyo
Copy link
Owner

sekoyo commented Feb 8, 2018

Hm strange, are you guys using webpack/babel in your projects or something else?

@sekoyo
Copy link
Owner

sekoyo commented Mar 28, 2018

Closing due to inactivity

@sekoyo sekoyo closed this as completed Mar 28, 2018
@sujayjaju
Copy link

@dominictobias

I am seeing the same thing. Though import * as ReactCrop from 'react-image-crop' did not work for.
onsole.log(ReactCrop) returns an object{}

How can I help debug?

@ssxv
Copy link

ssxv commented Oct 23, 2018

using a very specific version 5.0.0 worked for me.

@sekoyo
Copy link
Owner

sekoyo commented Oct 23, 2018

@sujayjaju You shouldn't do import * as this will return an object of all exports, instead import the default export with import ReactCrop from 'react-image-crop' as per the readme.

@ssxv How are you importing the component?

@ssxv
Copy link

ssxv commented Oct 23, 2018

I m doing
import * as ReactCrop from 'react-image-crop';

as suggested by above comments

@sekoyo
Copy link
Owner

sekoyo commented Oct 23, 2018

@ssxv in v6 do import ReactCrop from 'react-image-crop', this sometimes didn't work in previous versions due to the library being exported as a CommonJS module depending on your build setup

@sujayjaju
Copy link

@dominictobias Yes, I am using import ReactCrop from 'react-image-crop' - I also tried import * since that seemed to work for @danielasy

console log for ReactCrop return Object {} in both cases. The error states:
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

@sujayjaju
Copy link

So I tried deleting node_modules and doing a fresh install.
Got this error with ReactCrop:

ERROR in ./node_modules/react-image-crop/lib/ReactCrop.js 177:8
Module parse failed: Unexpected token (177:8)
You may need an appropriate loader to handle this file type.
|
| class ReactCrop extends PureComponent {
>   state = {}
|
|   componentDidMount() {
 @ dll reactBoilerplateDeps

@dominictobias Does that provide a clue?

@sekoyo
Copy link
Owner

sekoyo commented Oct 24, 2018

It seems like your build tool is picking up the jsnext:main entry which is ES6 code - https://github.com/DominicTobias/react-image-crop/blob/master/package.json#L7 instead of the normal "main" entry. I think I will remove that entry entirely as I've recently read it causes problems like this.

@sekoyo
Copy link
Owner

sekoyo commented Oct 24, 2018

For now try import ReactCrop from 'react-image-crop/dist/ReactCrop'

sekoyo added a commit that referenced this issue Oct 24, 2018
@sekoyo
Copy link
Owner

sekoyo commented Oct 24, 2018

@sujayjaju please try 6.0.1

@sujayjaju
Copy link

@dominictobias
Thanks for getting back so quickly. 6.0.1 works!

@maxsize
Copy link

maxsize commented Nov 1, 2018

Hi guys, I have the same problem here, I am using ts in my project, after some research, I find that I have to do something like this to make it work:
import * as ReactCrop from 'react-image-crop'; render() { const Component = (ReactCrop as any).default; return ( <Component src={src} ... /> ) }
It seems like something is wrong in the definition file.
help this helps.

@sekoyo
Copy link
Owner

sekoyo commented Nov 1, 2018 via email

@maxsize
Copy link

maxsize commented Nov 8, 2018

Hi @dominictobias , I am using 6.0.4.

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

6 participants