0

This is a simple piece of code. I am trying to get my first component to run and I followed the tutorial and this is their code for creating the component.

It works in that it does inject the first into the html document but the is missing.

For some reason it worked perfectly for the instructor in the video, as you can see here: https://youtu.be/G40iHC-h0c0?t=392

import React from "react";
import { render } from "react-dom";

class basicDiv extends React.Component {
    render(){
        return(
            <div>
                <h1>Hi my friend</h1>
            </div>
        );
    }
}

render(<basicDiv/>, window.document.querySelector(".componentPlaceHolder"));

here is the index.html file:

<!doctype html>

<html>

<head>

</head>
<body>
    <div class="componentPlaceHolder" style="border:1px solid black;min-height:100px;">

    </div>

    <script src="/app/bundle.js"></script>
</body>

</html>
6
  • Could you update your answer to show the relevant HTML of the page this is being rendered on? If you don't have an element with a class called .componentPlaceHolder then this won't render anything. Commented Jun 6, 2017 at 3:55
  • Are you getting any error in your console? Commented Jun 6, 2017 at 3:56
  • @Mr.Alien no errors. I do get the first <div> in the right spot. Just no <h1> Commented Jun 6, 2017 at 3:59
  • 1
    Possible duplicate of Html is not rendering in the browser - React js Commented Jun 6, 2017 at 5:01
  • 1
    yes it was a duplicate. Commented Jun 6, 2017 at 5:38

2 Answers 2

4

React component class has to be capitalised. Change that and your code will work. See this doc for more details on JSX.

class BasicDiv extends React.Component {
    render(){
        return(
            <div>
                <h1>Hi my friend</h1>
            </div>
        );
    }
}

ReactDOM.render(<BasicDiv/>, window.document.querySelector(".componentPlaceHolder"));
<!doctype html>

<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

</head>
<body>
    <h2>This is a Skeleton React Build</h2>
    <p>It uses babel to convert ES6 code to ES5</p>
    <p>This is a good place to start building your app. This is a blank canvas.</p>

    <h2>Below is where my first component will be injected</h2>
    <p>If you see anything in the box below... it works!</p>
    <div class="componentPlaceHolder" style="border:1px solid black;min-height:100px;">

    </div>

    <script src="/app/bundle.js"></script>
</body>

</html>

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

1 Comment

Thanks reaper. It's always the smallest of details that brings everything down.
1

D-reaper is correct. You should also return the render function to an ID in your html rather than to a .class

In your html change:

<div class=componentPlacholder>
to <div id="componentPlaceholder">`

Then at the bottom of your React component change:

render(<basicDiv/>, window.document.querySelector(".componentPlaceHolder"));
to render(<BasicDiv />, getElementById(componentPlacholder));

Don't forget to rename your component from basicDiv to BasicDiv as D-reaper mentioned

You can technically return to a .class but since it looks like you are learning it seemed helpful to alert you that you will cause yourself a lot of issues if you are not returning to an ID.

3 Comments

only use IDs? never classes? Will using classes break anything?
Its not that you can't and there will likely be some use cases where using a class could be helpful but only if you know exactly why you are choosing a class over the id. One of the great things about React compared to Ember or Angluar is that it is a lot less "opinionated" and therefore you can choose not to follow all of the conventions, however, its best to know exactly why you are choosing not to follow a convention and what the subsequent implications are before doing so.
Here is an example of using the class instead of id if it is helpful: blog.komand.com/…

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.