0

I am new to React JS and trying a very simple app. I have index.html file App.js and Header.js. I am trying to render Header in App and then rendering the whole App into the index.html. when App on its own without Header importing and rendering everything works just fine but when importing Header.js into App.js and render an instance of with everything in App.js it doesn't work at all. Can anybody explain me what's wrong with my code. Thank you.

/* Header Component in a Header.js file */

export class Header extends React.Component 
{
    render() 
    {
        return(
            <div>
                <h1>My Beautiful Website</h1>
                <hr />
            </div>
        );
    }
}



/* App Component in App.js file */

import Header from './Header';

class App extends React.Component 
{
    render()
    {
        return (
            <div>
               <Header />
                <section>
                    <h2>I am trying to make this React thing work</h2>
                    <p>
                        It should be easy to do I am trying to do but may be I am a little confused about 
                        things in React JS. Sooner or later though everything gets easier and most importantly
                        crystal clear to the human mind when you persevere. I believe.
                    </p>
                </section>
            </div>

        );
    }
}

ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.2/browser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/0.14.8/react-dom.min.js"></script>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>React Hello world</title>
</head>
<body>
    <div id="root"></div>

    <script src="./App.js" type="text/babel"></script>
    
</body>
</html>
knight
  • 625
  • 1
  • 6
  • 19
  • The problem is "named" vs "default" export. – Brian Thompson Jun 12 '20 at 20:20
  • Try: `export default class Header extends React.Component ` – Ajeet Shah Jun 12 '20 at 20:20
  • Import `Header` as a named export, i.e. `import { Header } from './Header';`, or switch to using a default export of the Header. – Drew Reese Jun 12 '20 at 20:20
  • I tried as you guys suggested i.e export default Header extends React.Component {} and then in App.js import Header from './Header.js'; and other way to leave of the default keyword and using the braces around Header in import statement in App.js but none of them worked. – knight Jun 12 '20 at 20:46
  • *but none of them worked* you'll have to explain a little bit more. What is the error? New error message? Same error message? – Brian Thompson Jun 12 '20 at 20:51
  • App.js:1 Uncaught ReferenceError: require is not defined at eval (eval at transform.run (browser.min.js:3), :15:15) at Function.transform.run (browser.min.js:3) at exec (browser.min.js:3) at browser.min.js:3 at XMLHttpRequest.xhr.onreadystatechange (browser.min.js:3) eval @ App.js:1 transform.run @ browser.min.js:3 exec @ browser.min.js:3 (anonymous) @ browser.min.js:3 xhr.onreadystatechange @ browser.min.js:3 XMLHttpRequest.send (async) transform.load @ browser.min.js:3 run @ browser.min.js:3 runScripts @ browser.min.js:3 – knight Jun 12 '20 at 20:53
  • When the the code doesn't work I get this message in chrome console. – knight Jun 12 '20 at 20:54
  • That error is unrelated – Brian Thompson Jun 12 '20 at 20:56
  • But nothing is displayed on the page when importing and rendering other component? – knight Jun 12 '20 at 21:05
  • @knight How did you [create react app](https://reactjs.org/docs/create-a-new-react-app.html)? Did you use a toolchain, like [CRA](https://reactjs.org/docs/create-a-new-react-app.html#create-react-app)? If you are creating everything from [scratch](https://reactjs.org/docs/create-a-new-react-app.html#creating-a-toolchain-from-scratch), you need to add a bundler like [webpack](https://stackoverflow.com/a/36255847/2873538). – Ajeet Shah Jun 12 '20 at 21:10
  • This could be one thing you are pointing out @AjeetShah, I am not using any bundler and the reason is I simply want to import react and babel in html file and create components in different js files to make it a simple working app in React JS . – knight Jun 12 '20 at 21:13
  • @knight [This](https://reactjs.org/docs/add-react-to-a-website.html) has all the steps and example code to do that. You need to import your components like: `` – Ajeet Shah Jun 12 '20 at 21:22
  • Did put the component script src in the head section and also in body section before the app.js script file but still not showing anything. – knight Jun 12 '20 at 21:58

0 Answers0