Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React starter template render nothing

<body>
    <div id="content"></div>

    <script type="text/jsx">
     var x = React.createClass({
        render:function(){
            return (
                <h1>I love react</h1>
            );
        }
     });

     React.render(<x/>,document.getElementById('content'));
    </script>
</body>

What's wrong with my reactjs code below? it render nothing. I've included

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react.min.js"></script>

in the head.

like image 955
Alien Xu Avatar asked Feb 04 '26 21:02

Alien Xu


2 Answers

You have several mistakes

  1. you need include ReactDOM and use ReactDOM.render, because since version 0.14.* .render method that located in React is deprecated.
  2. to define component you should use local variable that starts with an upper-case letter (should be X not x)
  3. instead of type="text/jsx" you should use type="text/babel" (don't forget include babel)

Example

like image 123
Oleksandr T. Avatar answered Feb 06 '26 10:02

Oleksandr T.


<script type="text/jsx"> won't work. This was how old versions did using the old jsx transformer but it's now deprecated.

Use babel instead. Read more about this topic here.

Also your components' names should always start with a capital letter. <X /> not <x />.

like image 38
Louay Alakkad Avatar answered Feb 06 '26 11:02

Louay Alakkad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!