-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
A more descriptive createElement warning #7402
Changes from 3 commits
3d1187e
b9dba9e
e782ed5
914f535
c08f81d
037818b
d01e2eb
3662819
6390b8d
056666d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
--- | ||
title: Invalid type for createElement | ||
layout: single | ||
permalink: warnings/create-element-types.html | ||
--- | ||
|
||
You probably came to this page because of this error: | ||
|
||
``` | ||
Warning: React.createElement: type should not be null, undefined, boolean, or number. | ||
It should be a string (for DOM elements) or a ReactClass (for composite components). | ||
``` | ||
|
||
This usually occurs when attempting to render an element that is of an invalid type. The following examples will trigger this error: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most people will come to this page from the link provided in the warning message. Assuming that is true, I think a more informative message could be shown here without repeating the warning. Instead of:
You could write something like:
|
||
### Invalid types | ||
|
||
Ensure that your component is not of the following types: undefined, boolean, number or null. | ||
|
||
`Foo.js` | ||
|
||
```js | ||
let Foo; | ||
|
||
if (false) { | ||
Foo = () => <div />; | ||
} | ||
|
||
export default Foo; // Foo is undefined | ||
``` | ||
|
||
`App.js` | ||
|
||
```js | ||
import Foo from './Foo' | ||
|
||
class ReactApp extends Component { | ||
render() { | ||
return <Foo />; | ||
} | ||
} | ||
``` | ||
|
||
### Invalid member imports | ||
|
||
This happens when attempting to import a member as a default member, or importing a default member as a member. | ||
|
||
`Foo.js` | ||
|
||
```js | ||
export const Foo = () => { return <div /> } | ||
``` | ||
|
||
`App.js` | ||
|
||
```js | ||
import Foo from './Foo' // wrong! | ||
// correct: import { Foo } from './Foo'; | ||
|
||
class ReactApp extends Component { | ||
render() { | ||
return <Foo />; | ||
} | ||
} | ||
``` | ||
|
||
### Invalid or missing export | ||
|
||
Check that the component is exported properly with the keyword `export`. | ||
|
||
`Foo.js` | ||
|
||
```js | ||
const Foo = () => { return <div /> } // Foo needs to be exported | ||
``` | ||
|
||
`App.js` | ||
|
||
```js | ||
import { Foo } from './Foo' // Foo is undefined | ||
|
||
class ReactApp extends Component { | ||
render() { | ||
return <Foo />; | ||
} | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,7 +192,8 @@ var ReactElementValidator = { | |
validType, | ||
'React.createElement: type should not be null, undefined, boolean, or ' + | ||
'number. It should be a string (for DOM elements) or a ReactClass ' + | ||
'(for composite components).%s', | ||
'(for composite components). See ' + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of just adding a link here, I would rather add a separate condition checking specifically for |
||
'https://facebook.github.io/react/warning/create-element-types.html%s', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't read the actual proposal but assuming we move forward, 2 things:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 will do |
||
getDeclarationErrorAddendum() | ||
); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s call it
Invalid Element Type Warning
for consistency with other warning’s style. Same for filename, would beinvalid-element-type.md
.