设置Easy First React项目

时间:2018-07-30 17:05:05

标签: javascript html reactjs file babeljs

我在设置第一个极其简单的React项目时遇到了麻烦。根div出现在我的屏幕上,但Greeting对象(Hello world)没有显示。

这是HTML:

 <link rel="stylesheet"d type"text/css" href="re.css" />
<html>
<div id="root"></div>
</html>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>

<script type="text/babel">
class Greeting extends React.Component {
    render() {
        return (<p>Hello world</p>);
    }
}
ReactDOM.render(
    <Greeting />,
    document.getElementById('root')
);
</script>

以下是CSS(不重要):

 #root {
    width: 40%;
    height: 300px;
    margin-top: 3em;
    left: 30%;
    position: absolute;
    border: 3px solid black;

    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
  }

谢谢!

1 个答案:

答案 0 :(得分:0)

问题似乎是html结构,您应该将所有内容包装在内,

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

<head>
    <title>Document</title>
    <link rel="stylesheet" d type "text/css" href="re.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>

</head>

<body>
    <div id="root"></div>
    <script type="text/babel">
            class Greeting extends React.Component {
                render() {
            return (<p>Hello world</p>);
        }
    }
    ReactDOM.render(
        <Greeting />,
        document.getElementById('root')
    );
    </script>
</body>
</html>