我尝试了这个简单的ReactJS应用程序,它不会呈现任何内容。 我正在使用atom作为我的文本编辑器。
的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://fb/.me/react-15.1.0.js"></script>
<script src="https://fb/.me/react-dom-15.1.0.js"></script>
<script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/babel-
core/5.8.38/browser-polyfill.min.js"></script>
<meta charset="UTF-8">
<title>Hello World with React</title>
</head>
<body>
<div id="react-container"></div>
<script type="text/babel" src="script.js"></script>
</body>
</html>
的script.js
const { render } = ReactDOM;
render(
<h1 id='title'
className='header'
style={{backgroundColor: 'orange', color: 'white', fontFamily:
'verdana'}}>
Hello World
</h1>,
document.getElementById('react-container')
);
文件结构
reactapp
-index.html
-script.js
如果我使用旧版本的ReactJS或者script.js文件中有任何错误,请更正我。
我在plunker中尝试了一下,它在script.js文件中显示了一些错误
答案 0 :(得分:0)
为Reactjs尝试这个最小的HTML模板。希望这对你有用。
<!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>
答案 1 :(得分:0)
您应该使用babel-standalone源。 改变自:
<script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.38/browser-polyfill.min.js"></script>
到
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
答案 2 :(得分:0)
我注意到的一些事情。通常在使用Babel进行转换时需要react和react-dom库:
const React = require('react');
const {render} = require('react-dom');
或者在使用import语句时:
import React from "react";
import ReactDOM, {render} from "react-dom";
但是plunker和其他代码编辑的工作方式有点不同。由于某种原因,我无法让它在plunker中工作,但在CodePen中查看相同内容:https://codepen.io/Jeff518Code/pen/zRwqzj
还要确保引用样式属性(我在CodePen中已经这样做了)
<h1 id='title' className='header' style={{'backgroundColor': 'orange', 'color': 'white', 'fontFamily': 'Verdana'}}>