Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object

前端 未结 30 1074
孤城傲影
孤城傲影 2020-11-22 06:53

I am getting this error:

Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/funct

相关标签:
30条回答
  • 2020-11-22 07:34

    I had an issue with React.Fragment, because the version of my react was < 16.2, so I got rid of it.

    0 讨论(0)
  • 2020-11-22 07:34

    I'm getting almost the same error:

    Uncaught (in promise) Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

    I was trying to import a pure functional component from another node library my team has developed. To fix it, I had to change to an absolute import (referencing the path to the component inside node_modules) from

    import FooBarList from 'team-ui';

    to

    import FooBarList from 'team-ui/lib/components/foo-bar-list/FooBarList';

    0 讨论(0)
  • 2020-11-22 07:36

    I ran into this error when I had a .jsx and .scss file in the same directory with the same root name.

    So, for example, if you have Component.jsx and Component.scss in the same folder and you try to do this:

    import Component from ./Component

    Webpack apparently gets confused and, at least in my case, tries to import the scss file when I really want the .jsx file.

    I was able to fix it by renaming the .scss file and avoiding the ambiguity. I could have also explicitly imported Component.jsx

    0 讨论(0)
  • 2020-11-22 07:36

    And in my case I was just missing a semicolon at the import-decleration in one of my sub modules.

    Fixed it by changing this:

    import Splash from './Splash'
    

    to this:

    import Splash from './Splash';
    
    0 讨论(0)
  • 2020-11-22 07:37

    In my case I was using the default export, but not exporting a function or React.Component, but just a JSX element, i.e.

    Error:

    export default (<div>Hello World!</div>)
    

    Works :

    export default () => (<div>Hello World!</div>)
    
    0 讨论(0)
  • 2020-11-22 07:38

    I got this by doing import App from './app/'; expecting it to import ./app/index.js, but it instead imported ./app.json.

    0 讨论(0)
提交回复
热议问题