Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught Error:expected a string. You forgot export your component from the file its defined,or you might have mixed up default/named import

Could you please help me to resolve the issue. I have already tried resolving the import functions but i still get that error and I have also tried to remove "{}" which didnt work. Thanks in advance. I am following TylerMcginnis React-Redux course. Navigation.js

import React from 'react'
import PropTypes  from 'prop-types'
import Link  from 'react-router-dom'
import { container, navContainer, link } from './styles.css'

Navigation.propTypes = ActionLinks.propTypes = NavLinks.propTypes = {
  isAuthed: PropTypes.bool.isRequired,
}

function NavLinks ({isAuthed}) {
  return (isAuthed === true
    ? <ul>
        <li><Link to='/' className={link}>{'Home'}</Link></li>
      </ul>
    : <noscript />)
}

function ActionLinks ({isAuthed}) {
  return (isAuthed === true
    ? <ul>
        <li>NEW DUCK</li>
        <li><Link to='/logout' className={link}>{'Logout'}</Link></li>
      </ul>
    : <ul>
        <li><Link to='/' className={link}>{'Home'}</Link></li>
        <li><Link to='/auth' className={link}>{'Authenticate'}</Link></li>
      </ul>)
}

export default function Navigation  ({isAuthed}) {
  return (
    <div className={container}>
      <nav className={navContainer}>
        <NavLinks isAuthed={isAuthed} />
        <ActionLinks isAuthed={isAuthed} />
      </nav>
    </div>
  )
}

MainContainer.js

import React from 'react'
import  Navigation from '../../components/Navigation/Navigation'
import {container, innerContainer}  from './styles.css'
import createReactClass from 'create-react-class'


const MainContainer = createReactClass({
  render () {
    return (
      <div className={container}>
        <Navigation isAuthed={true} />
        <div className={innerContainer}>
          {this.props.children}
        </div>
      </div>
    )
  },
})

export default MainContainer

error: 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, or you might have mixed up default and named imports. at invariant (invariant.js:42)

like image 401
Harsha Avatar asked Jan 21 '18 02:01

Harsha


People also ask

How do you fix element type is invalid expected a string for built in components or a class function for composite components but got undefined?

To solve the error "Element type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got", make sure to import named exports using curly braces and default exports without, and only use functions or classes as components.

What is export default react?

export default is used to export a single class, function or primitive from a script file. The export can also be written as export default class HelloWorld extends React.Component { render() { return <p>Hello, world!</


3 Answers

In my case, I was importing component as below so i was getting same error.

import { Calculator } from './src/calculator';

To fix it, i modified import as below and it worked.

import Calculator from './src/calculator';
like image 93
immirza Avatar answered Oct 09 '22 19:10

immirza


@Harsha I had the same error. It took me an hour or so of hunting around making sure my code was correct, and so forth. I started tearing down the code I had, to make it as simple as possible, and then simpler still.

Finally, the issue was that I had copied an empty file into my component directory, but was coding in an identically named file in another directory. So I was importing an empty file. Of course it was going to be undefined. :(

Also, remember to put in semi-colons. ;)

like image 33
NotoriousWebmaster Avatar answered Oct 09 '22 21:10

NotoriousWebmaster


The same hapenned to me. You are probably not saving the file. Maybe you are saving the App.js file but not the component file. I was saving the current file (ctrl+S) thinking I was saving all the files... I am using VS Code so I went to File/Preferences/Keyboard shortcuts and I set "Save all" to ctrl+shift+S.

like image 33
Egon Kirchof Avatar answered Oct 09 '22 20:10

Egon Kirchof