[英]React 'Hello World' Is not Loaded
我对React非常陌生,因为这是我的第一课。 以下是我的代码:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<meta charset="UTF-8">
<title>Hello World with React</title>
</head>
<body>
<div id= "react-container"></div>
<script src="index.js"></script>
</body>
</html>
index.js:
const title = React.createElement(
'h1'
{id: 'title', className: 'header'},
'Hello World'
)
ReactDOM.render(
title,
document.getElementById('react-container')
)
但是,浏览器中没有任何内容。 脚本和HTML页面都在同一文件夹中。 肯定有一个非常简单的错误,所以请有人帮助我吗? 谢谢你的时间!
在您的'h1'
之后,您缺少,
const title = React.createElement( 'h1', { id: 'title', className: 'header' }, 'Hello World' ) ReactDOM.render( title, document.getElementById('react-container') ) /**/
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="react-container"></div>
您只是在createElement
缺少逗号。 语法是
React.createElement(component, props, ...children)
const title = React.createElement( 'h1', { id: 'title', className: 'header' }, 'Hello World' ) ReactDOM.render( title, document.getElementById('react-container') )
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="react-container"></div>
用React祝你好运!
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Hello World</title> <script src="https://unpkg.com/react@16/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script> </head> <body> <div id="root"></div> <script type="text/babel"> ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('root') ); </script> <!-- Note: this page is a great way to try React but it's not suitable for production. It slowly compiles JSX with Babel in the browser and uses a large development build of React. To set up a production-ready React build environment, follow these instructions: * https://reactjs.org/docs/add-react-to-a-new-app.html * https://reactjs.org/docs/add-react-to-an-existing-app.html You can also use React without JSX, in which case you can remove Babel: * https://reactjs.org/docs/react-without-jsx.html * https://reactjs.org/docs/cdn-links.html --> </body> </html>
没有,
后hi
字符串。 把它
const title = React.createElement(
'h1',
{id: 'title', className: 'header'},
'Hello World'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.