Skip to content

Uncaught TypeError: Cannot read property '_mockedReactClassConstructor' of undefined #2828

@rogierschouten

Description

@rogierschouten

The error above has caught me twice now, and it's hard to debug. Perhaps an extra check could make this less hard to debug.

This is the location where the error happens:

function instantiateReactComponent(element, parentCompositeType) {
  var instance;

  if ("production" !== process.env.NODE_ENV) {
    ("production" !== process.env.NODE_ENV ? warning(
      element && (typeof element.type === 'function' ||
                     typeof element.type === 'string'),
      'Only functions or strings can be mounted as React components.'
    ) : null);

    // Resolve mock instances

    // ERROR OCCURS HERE BECAUSE type IS UNDEFINED:
    if (element.type._mockedReactClassConstructor) {

      // If this is a mocked class, we treat the legacy factory as if it was the
      // class constructor for future proofing unit tests. Because this might

The cause is always using an undefined variable in a JSX tag, like so:

var OtherComponent=undefined;
mycomp.render = function() {
    return (<OtherComponent/>);
}

The reason I get this error is because I often forget that I export OtherComponent directly using

module.exports=React.createClass(...);

and then I import it like so

var otherComponent = require("other-component");
var OtherComponent = otherComponent.OtherComponent;

which is wrong but not obviously so.

It would help very much to have an assertion on the first parameter of React.createElement() right away when it is called.

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