Skip to content

Warn when second argument to render is null and document.body is null as well #2448

@gaearon

Description

@gaearon

Too often I run into this:

 Invariant Violation: _registerComponent(...): Target container is not a DOM element.

when making a new project / example.

The reason is I'm passing document.body but React's script is in head and body doesn't exist yet.

This is very common problem as I observed for beginners, as they tend to put their scripts in head but React samples use document.body as mounting point.

The real problem here is the message: you wouldn't believe that document.body "is not a DOM element" and assume something else is wrong, without realizing it may be empty at the time.

I suggest we show a different, more specific warning when second argument is null and document.body is null. Something like "Target container is null. Are you trying to mount to document.body before it is ready?"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions