Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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; 
like image 489
Pankaj Thakur Avatar asked Dec 07 '15 09:12

Pankaj Thakur


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 an invariant violation?

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, or you might have mixed up default and named imports.


2 Answers

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.

like image 101
JohnL Avatar answered Sep 29 '22 11:09

JohnL


you need export default or require(path).default

var About = require('./components/Home').default 
like image 32
jackypan1989 Avatar answered Sep 29 '22 12:09

jackypan1989