Skip to content

Making React warn the user when createElement is called with undefined for the type argument #2607

@gabrielecirulli

Description

@gabrielecirulli

I spent the last hour trying to fix an issue that was caused by me trying to render a CSSTransitionGroup when the actual variable was undefined.

The root cause was me misinterpreting that require("react/addons") would return the whole React instead of just React.addons (which is my bad), but the error itself and the stack trace were extremely cryptic and it took me a while to even get a clue that the reason why it wasn't working was that I was just trying to render an undefined type.

This is the actual exception:

Uncaught TypeError: Cannot read property 'defaultProps' of undefined

And here's the stack trace:

developer tools - http___localhost_3000_requests_new from_template 3

You can see that, ultimately, execution comes crashing down when React tries to look at defaultProps on my undefined type, which of course raises an exception.

The reason why this was confusing to me is that the exception happened quite far away from the source cause and, as a secondary factor, this code worked completely fine before I started porting it to Browserify and made my mistake with react/addons. This lead me to a wild set of hypotheses as to why it wasn't working, such as assuming it might be due to a regression in React 0.12 (since it's been migrating from React.renderComponent to React.render and I thought CSSTransitionGroup might have been lagging behind), to thinking it could be a problem with CSSTransitionGroup itself.

The reason why I raise this issue is that, since passing an undefined type won't work anyway, it would make sense if React could check for undefined and warn about it or maybe even raise an exception (since it would crash soon after anyway).

My instance was a bit more extreme and if something like this happens to someone else they'll probably know where they screwed up in the first place, but some help from React would always be welcome!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions