REACT - Simple Intro component not rendering?

Hey I’ve been trying React, total newbie but this component mainIntro.js is not getting imported into the main react component App.js. I’d appreciate if anyone can help me figure out how this works !

EDIT: I’m using CDN links for React and ReactDOM because I was trying this out on a friend’s laptop.

mainIntro.js

const mainIntro = props => (
    <div id="quote-box">
     <h1> Hunter x Hunter Quotes </h1>

     <div id="text">
        "When I say it doesn't hurt me, that means I can bear it."
     </div>

     <div id="author">
        - Killua Zoldyck
     </div>

     <button id="new-quote"> Next Quote </button>

     <a href="#" id="tweet-quote" target="_blank"> Tweet this quote </a>

    </div>
)

export default mainIntro;

App.js

import mainIntro from './components'

class App extends React.Component{
    render(){
        return(
            <mainIntro />
        );
    }
}


const mainNode = document.getElementById("quoter");
ReactDOM.render(<App />,mainNode);

React component names must start with a capital letter :wink:

I tried that and renamed the component everywhere to MAINintro . Yet , it’s not getting rendered .

I did a quick test on codepen and it’s rendering fine:

Maybe you forgot to define the “quoter” ?

It rendered in this way ! But I had the component in a different file called Mainintro,js and imported that. It doesn’t work that way ? Why is that ?

Did you use:

import MainIntro from './components/MainIntro.js'

and in MainIntro.js you need to import React:

import React from 'react';

const MainIntro = props => (
    <div id="quote-box">

    [.....]

    </div>
)

export default MainIntro;

Did that but won’t work. I think I can’t use import React from 'react'; because I’m using a CDN. Is that why ?

It’s probably because the way you are writing your component. You are using a functional component, but there’s an error in your code. It should be:

const MainIntro = props => {
  return (
    <div id="quote-box">
     //... rest of the code   
    </div>
  );
}

Notice that you are not using curly brackets after the arrow and you are not returning anything either. This is not a React thing, this is JavaScript syntax. I’d suggest you to read more about arrow functions and ES6 features since you are going to see them a lot in React apps.

Also use the documentation, I find React official docs to be very helpful unlike other documentations.

I thought so too ! I tried the exact same code with using return specifically and it still won’t render.

Can you provide me with the exact same code that you are currently using in both modules?

I’d recommend using codepen for first steps with react (since they make importing things easy). If you want to go further, consider a code editor like

and use

to setup your initial files.

React is a steep road, if you begin, so I agree with @Gilbert1391, get comfortable with ES6 features first!

This is the component it self

const MainIntro = props => {
    return(
    <div id="quote-box">
     <h1> Hunter x Hunter Quotes </h1>

     <div id="text">
        "When I say it doesn't hurt me, that means I can bear it."
     </div>

     <div id="author">
        - Killua Zoldyck
     </div>

     <button id="new-quote"> Next Quote </button>

     <a href="#" id="tweet-quote" target="_blank"> Tweet this quote </a>

    </div>
    );
}

export default MainIntro;

And this is the main file which Im trying to import the component above into

import MainIntro from './components/MainIntro.js';

class App extends React.Component{
    render(){
        return(
            <MainIntro />
        );
    }
}


const mainNode = document.getElementById("quoter");
ReactDOM.render(<App />,mainNode);

Also this is the index.html file

<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HxH Quote Generator</title>

    <!--CDN links below this line-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
    <!--CDNs end here-->
    
    <link rel="stylesheet" href="style.css">
    
</head>
<div id="quoter"><!-- React JSX comes in here--></div>
<script type=text/babel src="app.js"></script>
</html>

I am using VS Code and also I have a fair grip over ES6 and React ( theory ). I’ve finished the Front End Certification and this was for one of the final projects

are you importing React at the top of your <MainIntro> component, i.e.: import React from "react"; ?

Tried importing React in both the js files and no results.:frowning:

But I didn’t see the import in your snippet?, show me the entire code for your MainIntro component.

Take a look at that code, maybe you can see if something is missing in your code?

Yep it’s all the same. I’m pretty sure it’s not working due to the CDN. I’ll install React on this laptop ( it’s my friend’s ) and try again. I don’t know where to put the React app afterwards though ? I mean is Heroku the only way or are there easier ways ? I need to put the link on FCC’s certifcations page.

The example below is working:

<html>

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>
</head>
<body>
  <div id='quoter'></div>
  <script type='text/babel'  >



class App extends React.Component{
   render(){
       return(
           <MainIntro />
       );
   }
}

const MainIntro = props => (
   <div id="quote-box">
    <h1> Hunter x Hunter Quotes </h1>

    <div id="text">
       "When I say it doesn't hurt me, that means I can bear it."
    </div>

    <div id="author">
       - Killua Zoldyck
    </div>

    <button id="new-quote"> Next Quote </button>

    <a href="#" id="tweet-quote" target="_blank"> Tweet this quote </a>

   </div>
)



const mainNode = document.getElementById("quoter");
ReactDOM.render(<App />,mainNode);

 </script>
</body>
</html>

The problem seems to be you can’t import things when using a CDN. Take a look at the console: it’s saying “require is not defined” when you use

script type=text/babel src=“app.js”></script


You could also put your react app on github as well, if you upload the build.js file. Codepen also works :wink:

I’m not familiar with the CDN, the only way to be 100% sure is if you drag your code to codesanbox for example and see if it works. You can use GitHub to deploy your react app as long as it’s static site (no back-end stuff).

I thought you can host only one site per account. Got it. Thanks !