React.createElement: type is invalid — expected a string

One of the most common import/export errors when using React is the error:

React.createElement: type is invalid -- expected a string

This somewhat cryptic error usually has a simple solution. More often than note a quick notation fix can solve the issue.

What’s the problem?

The problem usually stems from the way your are exporting and importing a custom component.

For example, you may have created a custom component and then exported it as follows:

export class MyNewComponent extends React.Component { ... }

Quite simply, this code is placed at the bottom of your react code extending the default React component (React.Component) and the export here will allow you to import it into your parent component as follows:

import MyNewComponent from './MyNewComponent';

This is normally where the dreaded error appears:

createElement: type is invalid -- expected a string

What is the error telling me?

Let’s break it down.

The error is happening when you are creating a new element – in this case initiantiating your custom component using createElement.

The actual error is that it is expecting something different to be returned than what otherwise is being – in this case, it’s expecting a string.

The solution

Most of the time there is a really simple fix – the case of adding a ‘default’ statement to your export.

In your custom component go back to the export line at the bottom of the code and change it to read:

export default class MyNewComponent extends React.Component { ... }

Doesn’t look much different right? Try executing your code again and see if it works beautifully now. Of course, if it moves onto another error you might have a little more debugging to do.

Now, full disclosure – this will not work everytime you have this error. However, in my experience, more often than not this is a simple fix to the majority of times the createElement: type is invalid error is occurred. It’s a common error among newbies to React but can also catch out experienced pros who are trying to rush through the latest development (we’ve all been there!)

Let us know if it’s worked for you!

You may also like...

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x