Skip to content
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

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

Closed
rogierschouten opened this issue Jan 8, 2015 · 4 comments

Comments

@rogierschouten
Copy link

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.

@gaearon
Copy link
Collaborator

gaearon commented Jan 8, 2015

I think this is covered by #2726 (via #2607) in master and 0.13 alpha

@rogierschouten
Copy link
Author

@Gearon Yes I agree, thank you.

@zpao
Copy link
Member

zpao commented Jan 9, 2015

#2726 also got backported into 0.12.2 so you should be able to just update and see a warning.

@rogierschouten
Copy link
Author

Nice @zpao, thanks for pointing that out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants