0

Still struggle to just simple run simple react application (just client to play around with).

Build is ok (you can see config here, previous question )

When I run server - there is an error on client side:

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

at invariant (transformed.js:304)
at ReactCompositeComponentWrapper.instantiateReactComponent [as _instantiateReactComponent] (transformed.js:11912)
at ReactCompositeComponentWrapper.performInitialMount (transformed.js:27510)
at ReactCompositeComponentWrapper.mountComponent (transformed.js:27401)
at Object.mountComponent (transformed.js:4158)
at mountComponentIntoNode (transformed.js:12676)
at ReactReconcileTransaction.perform (transformed.js:5756)
at batchedMountComponentIntoNode (transformed.js:12698)
at ReactDefaultBatchingStrategyTransaction.perform (transformed.js:5756)
at Object.batchedUpdates (transformed.js:29031)

Here is the code:

index.js

var React = require('react');
var ReactDOM = require('react-dom');
var App = require('./components/App.js');

ReactDOM.render(
    <App />,
    document.getElementById('app')
);

index.html

<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>test</title>
    </head>
    <body>
        <div id='app'></div>
    </body>
</html>

App.js:

import React from 'react';
import ReactDOM from 'react-dom';
// import {render} from 'react-dom'; //didn't helped either

class App extends React.Component {
  render () {
    return (
      <div>
        <p> Hello React!</p>
      </div>
    );
  }
}

FYI:

Answer is correct, but also one thing. Don't forget to change all require on import, like here (in index.js):

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.js';

1 Answer 1

2

It’s telling you exactly what the problem is:

You likely forgot to export your component from the file it's defined in.

Change App.js similar to:

// ...
export default class App extends React.Component {
// ...

BTW, why are you using require in index.js and ES6 imports in App.js?

Sign up to request clarification or add additional context in comments.

7 Comments

wellp, I am really not used to write in ES6 style, force myself to do it
but little bit different - just this Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. not any mention of that I forgot to export smth
yep, I got in in my app folder
Then can you please try to import the App component with an import statement: import App from './components/App.js.
whoa, that worked out. I changed all require to import! but how come?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.