简体   繁体   English

未捕获的错误:不变违规:元素类型无效:应为字符串

[英]Uncaught Error: Invariant Violation: Element type is invalid: expected a string

  • 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.在我跳过 retun 方法后,它会转到warning.js。
  • after I step over warning.js if conditions its going to instantiateReactComponent.js在我越过warning.js之后,如果条件它将实例化ReactComponent.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. error invariant.js?f23e:39 Uncaught Error: Invariant Violation: Element type is invalid: 需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义。 Check the render method of sports-container .检查sports-container的渲染方法。

    render() { const { sportsType, sportsDevice, sportsWordings, id } = this.props;渲染() { 常量 { sportsType,sportsDevice,sportsWordings,id } = this.props; let sportsEvent = true;让sportsEvent =真;

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

    warning.js警告.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. /** * 给定一个 ReactNode,创建一个实际挂载的实例。 * * @param {ReactNode} node * @return {object} A new instance of the element's constructor. * * @param {ReactNode} 节点 * @return {object} 元素构造函数的新实例。 * @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);

Check your import / require statements in your file, as well as the export for the components you're importing/requiring.检查文件中的 import / require 语句,以及您正在导入/需要的组件的导出。 When I get errors like this, it's usually because I'm either importing it incorrectly (ES6), ie当我收到这样的错误时,通常是因为我要么错误地导入它(ES6),即

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.或者我可能错误地导出了它(忘记导出默认值,或者可能在我不打算导出时导出为默认值),或者我忘记完全导出组件。

在我的情况下,问题在于启用了 nextjs swcMinify ,如果大多数人的问题在于导入,我认为eslint-import插件将有助于识别错误的导入

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的错误:始终违反:元素类型无效:预期为字符串或类/函数,但得到:对象 - Uncaught Error: Invariant Violation: Element type is invalid: expected a string or a class/function but got: object 未捕获的错误:不变违规:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数但得到:object - Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object 不变违规:元素类型无效:应为字符串。 (图像错误) - Invariant Violation: Element type is invalid: expected a string. (Image error) 错误“不断违反:元素类型无效:预期为字符串 - Error " Invariant violation : Element type is invalid: expected a string React Error - Uncaught Invariant Violation:元素类型无效 - React Error - Uncaught Invariant Violation: Element type is invalid 不变违规:元素类型无效:应为字符串或类/函数 - Invariant Violation: Element type is invalid: Expected a string or a class/function 未捕获的错误:元素类型无效:应为字符串 - Uncaught Error: Element type is invalid: expected a string 未捕获的错误:元素类型无效:应为字符串 - Uncaught Error: Element type is invalid: expected a string 未捕获的不变违反:元素类型无效(反应,webpack,循环导入) - Uncaught Invariant Violation: Element type is invalid (react, webpack, circular imports) React-Router 1.0.0未捕获的错误:始终违反:元素类型无效 - React-Router 1.0.0 Uncaught Error: Invariant Violation: Element type is invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM