1
  • i am trying to debug my react code.
  • in my render method I try to put debugger and debugger.
  • after I step over retun method its going to warning.js.
  • after I step over warning.js if conditions its going to instantiateReactComponent.js
  • I am not sure why its going to different files. can you tell me why its going to different files.
  • not sure how to debug.
  • it would be great if you guys give me some explaination so that in future I can fix the error my self
  • providing the snippet of the code where it goes in step over function call each time

    error invariant.js?f23e:39 Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of sports-container.

    render() { const { sportsType, sportsDevice, sportsWordings, id } = this.props; let sportsEvent = true;

        debugger;
    
        if (sportsEvent === true) {
            return (
    

    warning.js

/** * Similar to invariant but only logs a warning if the condition is not met. * This can be used to log issues in development environments in critical * paths. Removing the logging code for production environments will keep the * same logic and follow the same code paths. */

var warning = emptyFunction;

if (process.env.NODE_ENV !== 'production') {
  warning = function (condition, format) {
    for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
      args[_key - 2] = arguments[_key];
    }

    if (format === undefined) {
      throw new Error('`warning(condition, format, ...args)` requires a warning ' + 'message argument');
    }

    if (format.indexOf('Failed Composite propType: ') === 0) {
      return; // Ignore CompositeComponent proptype check.
    }

    if (!condition) {
      var argIndex = 0;
      var message = 'Warning: ' + format.replace(/%s/g, function () {
        return args[argIndex++];
      });
      if (typeof console !== 'undefined') {
        console.error(message);
      }
      try {
        // --- Welcome to debugging React ---
        // This error was thrown as a convenience so that you can use this stack
        // to find the callsite that caused this warning to fire.
        throw new Error(message);
      } catch (x) {}
    }
  };
}

instantiateReactComponent

  instance.construct(node);

/** * Given a ReactNode, create an instance that will actually be mounted. * * @param {ReactNode} node * @return {object} A new instance of the element's constructor. * @protected */

function instantiateReactComponent(node) {
  var instance;

  if (node === null || node === false) {
    instance = new ReactEmptyComponent(instantiateReactComponent);
  } else if (typeof node === 'object') {
    var element = node;
    !(element && (typeof element.type === 'function' || typeof element.type === 'string')) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Element type is invalid: expected a string (for built-in components) ' + 'or a class/function (for composite components) but got: %s.%s', element.type == null ? element.type : typeof element.type, getDeclarationErrorAddendum(element._owner)) : invariant(false) : undefined;

    // Special case string values
    if (typeof element.type === 'string') {
      instance = ReactNativeComponent.createInternalComponent(element);
    } else if (isInternalComponentType(element.type)) {
      // This is temporarily available for custom components that are not string
      // representations. I.e. ART. Once those are updated to use the string
      // representation, we can drop this code path.
      instance = new element.type(element);
    } else {
      instance = new ReactCompositeComponentWrapper();
    }
  } else if (typeof node === 'string' || typeof node === 'number') {
    instance = ReactNativeComponent.createInstanceForText(node);
  } else {
    !false ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Encountered invalid React node of type %s', typeof node) : invariant(false) : undefined;
  }

  if (process.env.NODE_ENV !== 'production') {
    process.env.NODE_ENV !== 'production' ? warning(typeof instance.construct === 'function' && typeof instance.mountComponent === 'function' && typeof instance.receiveComponent === 'function' && typeof instance.unmountComponent === 'function', 'Only React Components can be mounted.') : undefined;
  }

  // Sets up the instance. This can probably just move into the constructor now.
  instance.construct(node);
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
  • The return value of your render call is cut off in your question. – azium Apr 08 '16 at 14:15
  • @azium can you tell me why its jumping between different files when I do step over function in debuggers –  Apr 08 '16 at 14:25
  • Because functions are defined in different files? And because functions can get passed around as callbacks, so the callee can also be in a different file. I think your problem is not too difficult to solve, but if you don't show us your entire `render` function we'll never know. – azium Apr 08 '16 at 17:34
  • @all why did you guys mark it negative...I tried but not able to find the solution :( –  Apr 08 '16 at 18:08
  • @azium hey but I didn't write warning.js but when I debug its going there do you know why...i understood I posted only half of my render method since I am more interested in developer tools debugging :( –  Apr 08 '16 at 18:10
  • Just because you didn't write something doesn't mean your code doesn't use it. React has lots of files so of course the dev tools will jump into React functions. I didn't downvote you, but you're probably getting them because your question was very poorly formatted. And lastly, can you post your full render method or are you going to hide it from us on purpose? – azium Apr 08 '16 at 18:14

2 Answers2

26

Check your import / require statements in your file, as well as the export for the components you're importing/requiring. When I get errors like this, it's usually because I'm either importing it incorrectly (ES6), i.e.

import MyComponent from './my-component'

instead of

import { MyComponent } from './my-component'

or maybe I've exported it incorrectly (forgetting to export default, or maybe exporting as default when I didn't mean to), or I forgot to export the component entirely.

James Nail
  • 1,541
  • 1
  • 14
  • 23
0

In my Case the problem was with nextjs swcMinify enabled, in case of Most people the problem is with imports, I think eslint-import plugin will help to identify bad imports

Amin
  • 1,637
  • 1
  • 22
  • 40