javascript 为什么我的 react html 渲染不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32827092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:48:21  来源:igfitidea点击:

Why my react html render is not working?

javascriptreactjs

提问by Neha Sharma

I have written a simple react component but it is not rendering the HTML on the page. Could someone please guide me where I am doing wrong?

我编写了一个简单的 React 组件,但它没有在页面上呈现 HTML。有人可以指导我哪里做错了吗?

http://codepen.io/NehhaSharma/pen/EVNdJJ?editors=101

http://codepen.io/NehhaSharma/pen/EVNdJJ?editors=101

Thanks.

谢谢。

<div id="content"></div>
<script type="text/jsx">

var newComponent = React.createClass({
    render : function(){
        return (
            <h2>My Name is React</h2>
        );
    }

});

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

回答by duncanhall

Your component name should start with an upper-case letter. From the React Docs:

您的组件名称应以大写字母开头。来自React 文档

To render a React Component, just create a local variable that starts with an upper-case letter:

要渲染 React 组件,只需创建一个以大写字母开头的局部变量:

The following will render as expected:

以下将按预期呈现:

<div id="content"></div>
<script type="text/jsx">

var NewComponent = React.createClass({
    render : function(){
        return (
            <h2>My Name is React</h2>
        );
    }

});

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