最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Simple ReactJs Example not working - Stack Overflow

programmeradmin3浏览0评论

Getting issue with simple ReactJs Example using from react website :

First i imported :

<script src=".14.3.min.js"></script>
<script src=".14.3.min.js"></script>

Then i copy and pasted code :

// tutorial1.js
var CommentBox = React.createClass({
    render: function() {
        return (
            <div>
                <h1>Welcome</h1>
            </div>
        );
    }
});
ReactDOM.render(<CommentBox/>, document.body);

When opened page nothing show. Also tried :

document.getElementById('example')

No error in console don't no what issue is.

I am using jquery in page and using Laravel with Blade templates let me know if that is issue.

Also tried to use external script :

<script src="/lib/react-example.js" type="text/jsx"></script>

This is my Live Server you can check here.

Thanks

Getting issue with simple ReactJs Example using from react website :

First i imported :

<script src="https://fb.me/react-0.14.3.min.js"></script>
<script src="https://fb.me/react-dom-0.14.3.min.js"></script>

Then i copy and pasted code :

// tutorial1.js
var CommentBox = React.createClass({
    render: function() {
        return (
            <div>
                <h1>Welcome</h1>
            </div>
        );
    }
});
ReactDOM.render(<CommentBox/>, document.body);

When opened page nothing show. Also tried :

document.getElementById('example')

No error in console don't no what issue is.

I am using jquery in page and using Laravel with Blade templates let me know if that is issue.

Also tried to use external script :

<script src="/lib/react-example.js" type="text/jsx"></script>

This is my Live Server you can check here.

Thanks

Share Improve this question edited Nov 27, 2015 at 19:45 Earon asked Nov 27, 2015 at 19:39 EaronEaron 8232 gold badges13 silver badges18 bronze badges 1
  • Try wrap ReactDOM.render with Immediately-Invoked Function eg. $(function() {ReactDOM.render(<CommentBox/>, document.body);}) – Rafal Lesniak Commented Nov 27, 2015 at 19:47
Add a comment  | 

1 Answer 1

Reset to default 19

If you want to use the JSX syntax, you need to mark your script tags with type=text/babel and also load the babel browser script with:

<script src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>

<script type="text/babel">
var CommentBox = React.createClass({
    render: function() {
        return (
            <div>
                <h1>Welcome</h1>
            </div>
        );
    }
});
ReactDOM.render(<CommentBox/>, document.body);
</script>

Here is an example on jsfiddle, unfortunately it doesn't work on StackOverflow snippets.

Another option is to transcompile your .jsx it to javascript on the server side using a solution such as browserify or webpack.

发布评论

评论列表(0)

  1. 暂无评论