813

I am getting this error:

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

This is my code:

var React = require('react')
var ReactDOM =  require('react-dom')
var Router = require('react-router')
var Route = Router.Route
var Link = Router.Link

var App = React.createClass({
  render() {
    return (
      <div>
        <h1>App</h1>
        <ul>
          <li><Link to="/about">About</Link></li>
        </ul>
      </div>
    )
  }
})

var About = require('./components/Home')
ReactDOM.render((
  <Router>
    <Route path="/" component={App}>
      <Route path="about" component={About} />
    </Route>
  </Router>
), document.body)

My Home.jsx file:

var React = require('react');
var RaisedButton = require('material-ui/lib/raised-button');

var Home = React.createClass({
  render:function() {
    return (
        <RaisedButton label="Default" />
    );
  },
});

module.exports = Home;
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Pankaj Thakur
  • 9,481
  • 3
  • 23
  • 27
  • 12
    Please take a look at http://stackoverflow.com/questions/36795819/when-should-i-use-curly-braces-for-es6-import/36796281#36796281 – Marcelo Salazar Mar 08 '17 at 05:21
  • Possible duplicate of [When should I use curly braces for ES6 import?](https://stackoverflow.com/questions/36795819/when-should-i-use-curly-braces-for-es6-import) – Jim G. Jun 01 '18 at 16:39
  • This error can rise if **you try to import a non-existent component**. Make sure you have no typo and that the component indeed named that way. In case of libraries make sure you use the proper version, since components can have different names in different versions. – totymedli Jun 27 '18 at 03:16
  • This can also happen when you define a property, maybe through ES6 destruction, with the same name as an already imported component (and try to pass it down to another component). – Philip Murphy Jan 04 '19 at 21:46
  • This is happening to me as a result of using styled-components, and only server-side (using Next.js). For some reason, any `const` defined at the module level is magically `undefined` when referenced inside the React component. Could be a Rollup bug or a Node bug. – Steve Oct 15 '21 at 09:55
  • use `import { component } from "react"`instead of `import component from "react"` – Asma Feb 02 '22 at 13:20

52 Answers52

1229

In my case (using Webpack) it was the difference between:

import {MyComponent} from '../components/xyz.js';

vs

import MyComponent from '../components/xyz.js';

The second one works while the first is causing the error. Or the opposite.

e18r
  • 7,578
  • 4
  • 45
  • 40
JohnL
  • 13,682
  • 4
  • 19
  • 23
  • 109
    Googler from the future here, thanks this was my issue! It makes sense in retrospect as the first is destructuring the export, but if you used default, then it's just trying to destructure a function/class. – ebolyen Apr 05 '16 at 20:10
  • 4
    For future people, my case was was a typo in the element name I was using. I changed `` to `` – Ryan-Neal Mes Oct 25 '16 at 04:32
  • 8
    Huge. Why is this the case? – lol Dec 19 '16 at 10:27
  • 61
    For anyone still wondering why this works - {MyComponent} imports the export 'MyComponent' from the file '../components/xyz.js' - The second imports the *default* export from '../components/xyz.js'. – ShaunYearStrong Apr 05 '17 at 10:15
  • It may be also because of import inside MyComponent. in my case the error mentioned MyComponent, but actually import statement inside it cause the rror: import {SomeLibraryCompenent} from 'some-library' – ykorach Jul 18 '17 at 14:16
  • I inherited an app that exports all components from a components index file. If I create a component then try to import it from 'components' before I've exported it from that index file, I get this error. The same thing is happening, just trying to import something that isn't there - just another way that can happen that could be useful to check. – David Mason Mar 14 '18 at 04:51
  • 1
    Echoing what @ykorach said. For me it was because the component in question ("A") failed to import another separate component ("B") because that other component wasn't exported in its own file. The console log in Chrome Developer tools doesn't mention that, it only says "there's an error with "A", maybe you forgot to export that". In reality it's "B" that was never exported correctly, so check your development log carefully! – user2490003 May 03 '18 at 15:07
  • Expanding on @ShaunYearStrong comment, this is an error importing something that is exported. This solution fixes the *importer*, but the exporter may need updates. I fixed the import line, but my library needed to have `export default MyComponent` added – Charles L. Jul 29 '18 at 21:11
  • Neither works for me. Brackets or not. Imports are correct. Exports are correct. I'm pulling my hair out over this... – Elliot Tregoning Nov 29 '18 at 01:26
  • In my case the error was coming from MyComponent, but actually it was another component not being exported correctly. So make sure to check all the related components are being exported as well! – Simone Jan 22 '19 at 15:48
  • 7
    That's just great: if I write `import {App} from './App';`, the error is `'./App' does not contain an export named 'App'.`, and if I remove the curly brackets, the error becomes `Element type is invalid: expected a string ... but got: object.`. Welcome to ES import hell. – Francois Jan 28 '19 at 21:42
  • Can I pay you money for this ? You saved so much of my time – Jay Jay Jay Mar 23 '20 at 18:05
  • I was about to call a friend and offer him 50$ to solve this. Do you want the money instead :x – Lo-Tan Feb 25 '21 at 04:10
  • I had the same issue and the fix was changing my version of react-router-dom to "react-router-dom": "^5.0.1" in package.json – sreekmatta Mar 09 '22 at 03:31
198

you need export default or require(path).default

var About = require('./components/Home').default
jackypan1989
  • 2,786
  • 1
  • 13
  • 11
  • 2
    I had this issue with 'react-native-navbar'. I called default on the require and it fixed my issue. Thanks. – Varand Pezeshkian Dec 23 '15 at 23:29
  • 3
    Hmm, adding .default in my case solves the problem. However, I am actually export default Home extends Component on that class, so I would have expected it to work without the '.default' – Marc Mar 19 '16 at 14:19
  • When doing server-side rendering I has to add ".default" to require module for the component correctly before calling ```React.createFactory(importedComponent);``` – Nick Pineda Apr 28 '16 at 07:20
  • 3
    can u please explain the reason like what happens when we add default ? – Subham Tripathi Jul 23 '16 at 18:31
  • 1
    it worked but can can someone make an explanation about what .default does. – Burak Karasoy Aug 15 '16 at 08:40
  • 1
    @BurakKarasoy You need the .default since the standard way Babel build modules is to transform `export default` statements to `exports.default` so you have to use `.default` when you import the module. One way to get rid of that is to use [babel-plugin-add-module-exports](https://github.com/59naga/babel-plugin-add-module-exports) which restore the default behavior. – Jean-Baptiste Louazel Jan 27 '17 at 14:57
  • fast forward to 2023 and this fixed my problem – harriet Aug 10 '23 at 23:15
57

Have you just modularized any of your React components? If yes, you will get this error if you forgot to specify module.exports, for example:

non-modularized previously valid component/code:

var YourReactComponent = React.createClass({
    render: function() { ...

modularized component/code with module.exports:

module.exports = React.createClass({
    render: function() { ...
Soorena
  • 4,352
  • 5
  • 30
  • 42
charlez
  • 669
  • 4
  • 6
34

In my case, one of the exported child module was not returning a proper react component.

const Component = <div> Content </div>;

instead of

const Component = () => <div>Content</div>;

The error shown was for the parent, hence couldn't figure out.

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
Katti
  • 2,013
  • 1
  • 17
  • 31
30

If you get this error, it might be because you're importing link using

import { Link } from 'react-router'

instead, it might be better to use

import { Link } from 'react-router-dom'
                      ^--------------^

I believe this is a requirement for the react router version 4

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
NSCoder
  • 1,594
  • 5
  • 25
  • 47
  • 1
    My friend was importing Link from `react`, instead of `react-router-dom`. Fixed the issue. Never seen this err message before. – O-9 May 13 '19 at 13:34
  • 1
    I faced the same error in a different project, and the problem was with the `import` statement of `link`. – Anupam Dec 26 '20 at 16:55
  • 1
    Thankyou. I wasted 1 entire day figuring out which import/export was wrong. Finally this was what gave the error. I was migrating react-router v3 to v5 – rahulxyz Feb 12 '22 at 15:46
27

Given your error of:

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

You have 2 options:

  1. Your export file can have the word default as in
export default class someNameHere

Then your import will need to avoid using {} around it. As in

import somethingHere from someWhereHere
  1. Avoid using the default word. Then your export looks like
export class someNameHere

Then your import must use the {}. Like

import {somethingHere} from someWhereHere
jasonleonhard
  • 12,047
  • 89
  • 66
  • 3
    Awesome answer!!! +1... I totally overlooked the `export default {component name}` and forgot to add it – Si8 Jun 24 '19 at 15:08
  • @Si8 Glad to be of service and thanks for making me smile first thing in the morning. – jasonleonhard Jun 25 '19 at 19:08
  • Good answer. An explanation as to why that is so would really be helpful. – Timothy Macharia Aug 14 '21 at 15:20
  • 1
    Thank you, I was using `export const Foo = () => {return ()}` and completely forgot the keyword `default` – Solomon Antoine Dec 22 '21 at 20:35
  • 2
    @TimothyMacharia There are four forms of import declarations: 1. Named import: import { export1, export2 } from "module-name"; 2. Default import: import defaultExport from "module-name"; 3. Namespace import: import * as name from "module-name"; 4. Side effect import: import "module-name"; [REF](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) – jasonleonhard Oct 09 '22 at 01:38
24

Don't get surprised by the list of answers for a single question. There are various causes for this issue;

For my case, the warning was

warning.js:33 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check your code at index.js:13.

Followed by the error

invariant.js:42 Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

I couldn't understand the error since it doesn't mention any method or file name. I was able to resolve only after looking at this warning, mentioned above.

I have the following line at the index.js.

<ConnectedRouter history={history}>

When I googled for the above error with the keyword "ConnectedRouter" I found the solution in a GitHub page.

The error is because, when we install react-router-redux package, by default we install this one. https://github.com/reactjs/react-router-redux but not the actual library.

To resolve this error, install the proper package by specifing the npm scope with @

npm install react-router-redux@next

You don't need to remove the wrongly installed package. It will be automatically overwritten.

Thank you.

PS: Even warning helps you. Don't neglect warning just looking at the error alone.

Lex
  • 4,749
  • 3
  • 45
  • 66
Balasubramani M
  • 7,742
  • 2
  • 45
  • 47
  • Thank you your answer is great. The problem is not fully understood or resolved as you mentioned there are multiple reasons. In my case I had to use react-router-redux library but that alone was not sufficient, my jest was failing because I was using react-hot-loader, after removing hot(module) from "export default hot(module)(withRouter(MyComponent));" I was able to get my unit tests to run, which was my issue. Main code was never had the problem. So I assume some lib conflict with dependencies/jest/OS is the suspect. – MG Developer Oct 16 '20 at 19:41
21

https://github.com/rackt/react-router/blob/e7c6f3d848e55dda11595447928e843d39bed0eb/examples/query-params/app.js#L4 Router is also one of the properties of react-router. So change your modules require code like that:

  var reactRouter = require('react-router')
  var Router = reactRouter.Router
  var Route = reactRouter.Route
  var Link = reactRouter.Link

If you want to use ES6 syntax the link use(import), use babel as helper.

BTW, to make your code works, we can add {this.props.children} in the App, like

render() {
  return (
    <div>
      <h1>App</h1>
      <ul>
        <li><Link to="/about">About</Link></li>
      </ul>
      {this.props.children}
    </div>

  )
}
David Guan
  • 4,180
  • 1
  • 25
  • 32
  • Sorry for not test carefully last night, there is no problem in your Home component. Can you have a try what I just edited? – David Guan Dec 08 '15 at 02:31
13

In my case, that was caused by wrong comment symbols. This is wrong:

<Tag>
    /*{ oldComponent }*/
    { newComponent }
</Tag>

This is correct:

<Tag>
    {/*{ oldComponent }*/}
    { newComponent }
</Tag>

Notice the curly brackets

tuanh118
  • 311
  • 2
  • 5
12

I have the same error : ERROR FIX !!!!

I use 'react-router-redux' v4 but she's bad.. After npm install react-router-redux@next I'm on "react-router-redux": "^5.0.0-alpha.9",

AND IT'S WORK

Thibault Jp
  • 151
  • 1
  • 3
  • 8
10

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

tybro0103
  • 48,327
  • 33
  • 144
  • 170
10

I was having the same issue and realized that I was providing an Undefined React component in the JSX markup. The thing is that the react component to render was dynamically calculated and it ended up being undefined!

The error stated:

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of C.,

The example producing this error:

var componentA = require('./components/A');
var componentB = require('./components/B');

const templates = {
  a_type: componentA,
  b_type: componentB
}

class C extends React.Component {
  constructor(props) {
    super(props);
  }
  
  // ...
  
  render() {
    //Let´s say that depending on the uiType coming in a data field you end up rendering a component A or B (as part of C)
    const ComponentTemplate = templates[this.props.data.uiType];
    return <ComponentTemplate></ComponentTemplate>
  }
  
  // ...
}

The problem was that an invalid "uiType" was provided and therefore this was producing undefined as result:

templates['InvalidString']
cSn
  • 2,796
  • 3
  • 23
  • 29
  • I seem to have a similar error with react-fine-uploader, but cant figure out anything(yet). It has a Gallery component which i am importing and it goes to its render method too but in that render error occurs, i am not sure if it is library issue or my issue as i ma quite new to react. – Zia Ul Rehman Mughal Oct 03 '17 at 10:00
  • Had a similar error where there was an error in a complex component. A simple debug method is to temporarily simplify the component, e.g. `const MyComponent = () =>
    my component
    ;` just to see if the imports work normally then.
    – metakermit Oct 10 '17 at 10:32
  • It would be handy to be able to detect this, e.g. with a lint rule. – Will Sep 25 '19 at 14:23
8

Just as a quick addition to this. I was having the same problem and while Webpack was compiling my tests and the application was running fine. When I was importing my component into the test file I was using the incorrect case on one of the imports and that was causing the same error.

import myComponent from '../../src/components/myComponent'

Should have been

import myComponent from '../../src/components/MyComponent'

Note that the import name myComponent depends on the name of the export inside the MyComponent file.

GAEfan
  • 11,244
  • 2
  • 17
  • 33
Blackout1985
  • 121
  • 1
  • 4
7

Had similar issue with React Native latest versions 0.50 and up.

For me it was a difference between:

import App from './app'

and

import App from './app/index.js'

(the latter fixed the issue). Took me hours to catch this weird, hard to notice nuance :(

Olegdater
  • 2,381
  • 22
  • 20
6

Another possible solution, that worked for me:

Currently, react-router-redux is in beta and npm returns 4.x, but not 5.x. But the @types/react-router-redux returned 5.x. So there were undefined variables used.

Forcing NPM/Yarn to use 5.x solved it for me.

Florian Richter
  • 131
  • 2
  • 8
6

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

bergie3000
  • 1,091
  • 1
  • 13
  • 21
6

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>)
Andru
  • 5,954
  • 3
  • 39
  • 56
5

In my case, the import was happening implicitly due to a library.

I managed to fix it by changing

export class Foo

to

export default class Foo.

thedayturns
  • 9,723
  • 5
  • 33
  • 41
5

I got this error in react routing, problem was that I was using

<Route path="/" component={<Home/>} exact />

but it was wrong route requires component as a class/function so I changed it to

<Route path="/" component={Home} exact />

and it worked. (Just avoid the braces around the component)

4

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';
Rikard Askelöf
  • 2,762
  • 4
  • 20
  • 24
4

For me, it was because my component wasn't exporting for some reason using the * notation, so I had to name the export explicitly

// index.ts
export * from "./Button"

to

// index.ts
export { Button } from "./Button"
wongz
  • 3,255
  • 2
  • 28
  • 55
3

In addition to import/export issues mentioned. I found using React.cloneElement() to add props to a child element and then rendering it gave me this same error.

I had to change:

render() {
  const ChildWithProps = React.cloneElement(
    this.props.children,
    { className: `${PREFIX}-anchor` }
  );

  return (
    <div>
      <ChildWithProps />
      ...
    </div>
  );
}

to:

render() {
  ...
  return (
    <div>
      <ChildWithProps.type {...ChildWithProps.props} />
    </div>
  );
}

See the React.cloneElement() docs for more info.

Greg K
  • 10,770
  • 10
  • 45
  • 62
3

I was getting this error also. The error was being caused by trying to export a component like this...

export default Component();

Instead of like this...

export default Component;

My understanding is that by adding the "()" at the end of the component, I was actually calling the function instead of just referencing it.

I did not see this in the answers above, but may have missed it. I hope it helps someone and saves some time. It took me a long time to pinpoint the source of this error!

2

The problem can also be an alias used for a default export.

Change

import { Button as ButtonTest } from "../theme/components/Button";

to

import { default as ButtonTest } from "../theme/components/Button";

solved the issue for me.

Simon
  • 6,025
  • 7
  • 46
  • 98
2

I was the same problem because I did import incorrect library, so i checked the documentation from the library and the route was changed with the new versión, the solution was this:

import {Ionicons} from '@expo/vector-icons';

and I was writing the incorrect way:

import {Ionicons} from 'expo';
hackemate
  • 520
  • 4
  • 12
  • Yes, I want to shout this out too. We are all here from different angles I am sure. In my case, I had a library already installed (Stripe) and followed a guide from their website. They were using a new element that was not in the library I already had installed. Instead of a failed build due to the component not being there, my app built but when I manually checked the node_modules lib, the export I was trying to import in fact wasn't there. So I did an npm install modulename --latest and that fixed it. Seriously just check your node modules as unlikely as it seems. – Andy Oct 24 '21 at 22:14
2

I was getting this issue too. My imports look fine, I could copy the contents of my copy and paste it where it was being used and that worked. But it was the reference to the component that was going wrong.

For me I just had to shut down expo and restart it.

Sigex
  • 2,834
  • 2
  • 24
  • 25
2

Just want to add a case to this question. I walked around this issue by swapping the order of import, for example in my mixed of imports before:

import { Label } from 'components/forms/label';
import Loader from 'components/loader/loader';
...
import Select from 'components/select/select'; // <----- the error happen

After the change:

import Select from 'components/select/select'; // <----- Fixed
import { Label } from 'components/forms/label';
import Loader from 'components/loader/loader';
...
MocXi
  • 456
  • 5
  • 14
1

For me it was that my styled-components were defined after my functional component definition. It was only happening in staging and not locally for me. Once I moved my styled-components above my component definition the error went away.

Jadam
  • 1,650
  • 1
  • 19
  • 40
1

It means some of your imported Components are wrongly declared or nonexistent

I had a similar issue, I did

import { Image } from './Shared'

but When I looked into the Shared file I didn't have an 'Image' component rather an 'ItemImage' Component

import { ItemImage } from './Shared';

This happens when you copy code from other projects ;)

webmaster
  • 1,960
  • 24
  • 29
1

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

Eugene Ihnatsyeu
  • 1,299
  • 13
  • 15
1

Error? its definitely an import or export issue , you are reffering to a component or container that either you haven't exported from its defining script or when importing it u have used the wrong format.

Solution?

  1. Go through all your component/container definitions and make sure you have exported them at the end of the script i.e "export default 'yourcomponentname';" or at the beginning of the component definition i.e export const 'yourcomponentname'=() =>{ ....... }
  2. At your import statements make sure you have not mixed up named and default imports based on your corresponding export statements
bitbuoy
  • 353
  • 2
  • 8
  • OMG . I can't believe that not only I didn't export my component but I didn't define the whole component at all. sometimes we do silly mistakes . ty worked for me :) – amir_70 Jul 20 '22 at 12:31
1

The error was fixed when I changed this code

AppRegistry.registerComponent(appName, App );

to this code :

AppRegistry.registerComponent(appName, () => App);
amir behrouzi
  • 155
  • 1
  • 10
1

Under similar case, I had the same nasty error and I concentrated on the code but nothing above seemed to improve the situation. I was transferring from Javascript to Typescript but encountered that for some weird reason I had BOTH files at the same time. Removing the JS version solved it, that may save somebody's time.

CyberMessiah
  • 1,084
  • 11
  • 14
1

In my case was problem with npm-workspaces. I have imported badly something from different workspace.

honzzyk
  • 169
  • 2
  • 7
0

@Balasubramani M saved me here. Wanted to add one more to help people. This is the problem when you're gluing too many things together and being cavalier with versions. I updated a version of material-ui and need to change

import Card, {CardContent, CardMedia, CardActions } from "@material-ui/core/Card"; 

to this:

import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';
Kyle Pennell
  • 5,747
  • 4
  • 52
  • 75
0

I had the same issue and the issue was some js files were not included in the bundling of that specific page. Try including those file and problem might be fixed. I did this:

.Include("~/js/" + jsFolder + "/yourjsfile")

and it fixed up the issue for me.

This was while I was using React in Dot NEt MVC

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Jason
  • 41
  • 1
0

I've discovered another reason for this error. It has to do with the CSS-in-JS returning a null value to the top-level JSX of the return function in a React component.

For example:

const Css = styled.div`
<whatever css>
`;
export function exampleFunction(args1) {
  ...
  return null;
}

export default class ExampleComponent extends Component {
...
render() {
const CssInJs = exampleFunction(args1);
  ...
  return (
    <CssInJs>
      <OtherCssInJs />
      ...
    </CssInJs>
  );
}

I would get this warning:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: null.

Followed by this error:

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

I discovered it was because there was no CSS with the CSS-in-JS component that I was trying to render. I fixed it by making sure there was CSS being returned and not a null value.

For example:

const Css = styled.div`
<whatever css>
`;
export function exampleFunction(args1) {
  ...
  return Css;
}

export default class ExampleComponent extends Component {
...
render() {
const CssInJs = exampleFunction(args1);
  ...
  return (
    <CssInJs>
      <OtherCssInJs />
      ...
    </CssInJs>
  );
}
codeinaire
  • 1,682
  • 1
  • 13
  • 26
0

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

Patrick
  • 1,227
  • 14
  • 17
0

In my case it ended up being the outer component imported like this:

import React, { Component } from 'react';

and then declared like:

export default class MyOuterComponent extends Component {

where an inner component imported the React bare:

import React from 'react';

and dotted into it for declaration:

export default class MyInnerComponent extends ReactComponent {

krayzk
  • 877
  • 1
  • 10
  • 19
0

In my case, it took me a bunch of time to search out and checked may ways but only fixed by this way: remove "{", "}" when importing a custom component:

import OrderDetail from './orderDetail';

My wrong way was:

import { OrderDetail } from './orderDetail';

Because in the orderDetail.js file, I exported OrderDetail as the default class:

class OrderDetail extends Component {
  render() {
    return (
      <View>
        <Text>Here is an sample of Order Detail view</Text>
      </View>
    );
  }
}

export default OrderDetail;
tiennsloit
  • 81
  • 3
0

I got this error while upgrading all libs to latest version

in older version following are imports

import { TabViewAnimated, TabViewPagerPan } from 'react-native-tab-view';

but in new version they are replaced with following

import { TabView, PagerPan } from "react-native-tab-view";

So be check proper all your import are available by using control click

Rajesh N
  • 6,198
  • 2
  • 47
  • 58
0

In my case, it was the meta tags.

I was using

const MetaTags = require('react-meta-tags');

I guess MetaTags is a default export, So changing to this solved it for me, took hours to figure out which one was causing it.

const { MetaTags } = require('react-meta-tags');
Sathesh
  • 486
  • 2
  • 12
  • 29
0

exporting at the bottom of the file might solve it.

const IndicatorCloak = (props) => { ... }
export default IndicatorCloak;
ravibagul91
  • 20,072
  • 5
  • 36
  • 59
Noimang
  • 11
  • 1
0

By mistake i tried to import Container and Content from react-native instead of native-base.

Error because of this line:

import { ActivityIndicator, RefreshControl,  StyleSheet, View, Keyboard, Container, Content } from 'react-native';

Below is the code fix:

import { ActivityIndicator, RefreshControl,  StyleSheet, View, Keyboard } from 'react-native';
import { Container, Content} from 'native-base';
Sudheer Kumar Palchuri
  • 2,919
  • 1
  • 28
  • 38
0

In my case I had written const Tab = createBottomTabNavigator instead of const Tab = createBottomTabNavigator()

RonH
  • 504
  • 1
  • 3
  • 12
0

Are you tired of naive import/export answers while you're nesting components?

Well, I've been, too, when trying to pass in props from parent to child, and the child was supposed to render sibling component.

Let say, you have a BaseDialog (parent) component, in which you want to have another component SettingsDialog (child) in which you want to render its independent component.

BaseDialog.tsx

import BaseButton from "./../BaseButton";
...
<SettingsDialog
  BaseButton={<BaseButton title="Connect Wallet" />}
/>

and you're trying to render in somewhere in Settings.Dialog.tsx the

const SettingsDialog = (props: {
  BaseButton: any;
}) => {
  const { BaseButton } = props;

  return(
    <div>
      {BaseButton}
    </div>
  );
}

(yes, it's a handy way to pass components parent->child, especially when they grow, but here it breaks), but it'll cause the Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object error.

What you can do, to fix it, and still use the BaseButton, is to simply import it separately.

Settings.Dialog.tsx

import BaseButton from "./../BaseButton";

const SettingsDialog = () => {
  return(
    <div>
      <BaseButton title="Connect Wallet" />
    </div>
  );
}

and in parent simply

BaseDialog.tsx

<SettingsDialog />

What you're trying to achieve will be the same, and will fix this unintuitive, in this scenario, error.

In my examples, I've assumed that BaseDialog has a required prop with a name title.

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
0

In my case,

//details.js 
import React from 'react'

const Details = (props) =>{
return <h1>Details</h1>
}

export default Details;


//main.js
import React from "react";
import { withRouter } from "react-router";
import {Route,BrowserRouter as Router,Redirect,Switch,Link} from "react-router-dom";
import Details from "./myAccount/details";

const Main = (props) => {
 return (
   <>
        <Switch>
          <Route path="/my-account/details" component={<Details />} />
        </Switch>
   </>

 );
};

export default withRouter(Main);

===========================

Replaced with below one, error got fixed
component={<Details />}  with  component={Details} 
Kodali444
  • 1,283
  • 1
  • 17
  • 21
0

For me it was the name of app config json. In the index.js the imports were like this:

import { AppRegistry } from 'react-native'
import App from './app'
import { name as appName } from './app.json'

AppRegistry.registerComponent(appName, () => App)

React Native does not differentiate between './app' and './app.json'. No idea why. So I've changed the name of the app.json to app.config.json and this has solved the problem:

import { AppRegistry } from 'react-native'
import App from './app'
import { name as appName } from './app.config.json'

AppRegistry.registerComponent(appName, () => App)
Dareon
  • 374
  • 3
  • 13
0

Make sure you only return one PARENT CHILD

This will work

<div>
    <p> Something here </p>
</div>

This won't work

<div>
    <p> Something here </p>
</div>
<div>
    <p> Something else here </p>
</div>
Gabriel Arghire
  • 1,992
  • 1
  • 21
  • 34
0

I don't believe my situation and remedy are discussed here, so I'll add.

I have a function that returns an object called notification

import IconProgress from '../assets/svg/IconProgress';

  const getNotificationFromAttachment = (attachment) => {
    const notification = {
    Icon: IconProcessing,
    iconProps: {},
  };
  
  ...
  notification.Icon = {IconProgress};
  
  return notification
}

React expects a String or Class/Function as the value for the variable notification.Icon

So all you need to do is change {IconProgress} to IconProgress

See image below:

enter image description here

cshdev
  • 19
  • 3
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/32877724) – Greg Oct 12 '22 at 17:50
0

The problem might be in using an SVG as a component.

import { ReactComponent as HeaderLogo } from "../../assets/logoHeader.svg";
    
    const Header = () => {
        return (
            <div>
                <HeaderLogo />
            </div>
        );
    };

You can display an image this way:

import HeaderLogo from "../../assets/logoHeader.svg";

const Header = () => {
    return (
        <div>
            <img src={HeaderLogo} alt="React Logo" />
        </div>
    );
};
0

I had same error in React app,during practice in my console. Answer: Check your parameters or attributes are small or capital letters.And try to change it and run again. Or may be you forget to add component module. It solved my error. Try it.

Rushi Dave
  • 11
  • 2