javascript - Uncaught Error : Invariant Violation: Element type is invalid: expected a string

标签 javascript jquery reactjs react-native redux

  • 我正在尝试调试我的 React 代码。
  • 在我的渲染方法中,我尝试放置调试器和调试器。
  • 在我跨过 retun 方法后,它会转到 warning.js。
  • 在我跨过 warning.js if conditions 之后它会实例化 ReactComponent.js
  • 我不确定为什么它会转到不同的文件。你能告诉我为什么它会转到不同的文件吗?
  • 不确定如何调试。
  • 如果你们能给我一些解释就太好了,这样我以后就可以自己修复错误了
  • 提供每次单步执行函数调用的代码片段

    错误 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) 但得到:未定义查看sports-container的渲染方法。

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

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

    警告.js

/** * 与不变性类似,但仅在不满足条件时才记录警告。 * 这可用于记录关键开发环境中的问题 *路径。删除生产环境的日志记录代码将保留 * 相同的逻辑并遵循相同的代码路径。 */

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);

/** * 给定一个 ReactNode,创建一个将实际挂载的实例。 * * @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);

最佳答案

检查文件中的导入/要求语句,以及要导入/需要的组件的导出。 当我遇到这样的错误时,通常是因为我导入不正确 (ES6),即

import MyComponent from './my-component'

代替

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

或者我可能导出不正确(忘记导出默认值,或者可能在我无意的情况下导出为默认值),或者我忘记完全导出组件。

关于javascript - Uncaught Error : Invariant Violation: Element type is invalid: expected a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36501856/

相关文章:

javascript - 如何使用 Jquery/Ajax 调用将数据显示为表格?

javascript - 如果是全局窗口参数,为什么要传递全局窗口参数

javascript - 带有 Typescript 和 react-redux 的有状态组件 : Argument of type 'typeof MyClass' is not assignable to parameter of type Component

reactjs - 如何在 Apollo 客户端中使用没有 JSX 组件的 client.query?

css - 为什么设置了 dialogClassName 和使用 Styled JSX 的 Bootstrap 模态样式需要全局标记?

javascript - jQuery/HTML5 轮盘赌不工作

javascript - 将变量调用到不同的路径在 Nodejs 和 Express 中不起作用

javascript - CKEditor插件,如何在单击创建的内容时重新打开对话框

javascript - 如何将两个以上的参数传递给模态 ng2 bootstrap

jquery - 在jQuery中,设置具有相同类的span来显示:inline from :block