0

I cannot seem to figure out what I shall do to correct the issue:

I always got no display on the browser, where I expect to see "Hello React".

Here is the HelloWorld code:

ReactDOM.render(

Hello, React!

,document.getElementById('root'));

3
  • Does React automatically transform JSX? I think you should use babel, typescript.... for it. Commented Jul 18, 2017 at 9:52
  • I think you have missed a starting <body> tag in your HTML. And the script tags should be placed before the closing </body> tag. Commented Jul 18, 2017 at 9:53
  • Yep, the code was messed up when formatting on SO. It got worse when I tried to edit it later when I see your comment. Now it is even printing out THe result in SO, and missing out the code. Commented Jul 19, 2017 at 7:27

4 Answers 4

1

You can use following code base to understand basic of React JS (have been used latest version react-15.0.0) -

<!DOCTYPE html>
<html lang="en">

<head>
    <title>My First React Example</title>
   <!-- use this as script file "fb.me/react-15.0.0.js"
    "fb.me/react-dom-15.0.0.js"
    cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js" -->
</head>

<body>
    <div id="greeting-div"></div>
    <script type="text/babel">
        var Greeting = React.createClass({ 
            
    render: function() { 
        return ( <div>{this.props.children} </div>) 
    } 
        }); 
        
ReactDOM.render(
 <div>
        <Greeting>hello Aby</Greeting>
         <h1>HELLO</h1>
        </div>,
    document.getElementById('greeting-div') );
    </script>
</body>

</html>

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

Comments

0

check your example here, https://codesandbox.io/s/ElRmqWK5Y what I will suggest download the react package and run your code there in the beginning, and what I think about your code is you should add react library first than react-dom.

1 Comment

+Abhishek, can I ask why I'd download the react package first?
0

there is a missing < body > in your code , try to add it and tell us if it works

Comments

0

So basically the problem is that you don't transpile your jsx. For your simple example what you can do if you don't want to configure transpiling 'explicitly' with some bundler you can add Babel library and change

<script type="text/jsx"> 

to

<script type="text/babel"> 

Let's have a look here, but please remember that jsfiddle uses babel behind (quite sure):

https://jsfiddle.net/69z2wepo/83023/

Also let me paste example from babel docs. Especially take a look for added babel lib + script type.

<div id="output"></div>
<!-- Load Babel -->
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- Your custom script here -->
<script type="text/babel">
  const getMessage = () => "Hello World";
  document.getElementById('output').innerHTML = getMessage();
</script>

Comments

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.