使用Babel Standalone仅具有index.html和Component的单个React Component渲染 [英] Single React Component rendering with Babel Standalone with only index.html and Component
问题描述
Noob和React在这里.我在玩React.我有一个简单的组件可以在我的component.js中呈现.它包含在我的index.html文件中.我在头中包含了React
,ReactDOM
和babel
的脚本.我只想看到一个div正确渲染.我还没有使用Node,而是使用React和Babel进行练习(使用 babel-standalone ).我正在使用一个简单的http服务器运行文件.我的React Chrome扩展程序出现错误:Waiting for roots to load...to reload inspector click here
.
Noob with React here. I am playing around with React. I have a simple component to render inside my component.js. It is included in my index.html file. I included the scripts for React
, ReactDOM
, and babel
in the head. I just want to see that one div render properly. I am not using Node yet, just a exercise with React and Babel (using babel-standalone). I am running the file with a simple http-server. I am getting an error with the React Chrome extension: Waiting for roots to load...to reload inspector click here
.
index.html
<!DOCTYPE html>
<html>
<head>
<!-- React -->
<script src="https://unpkg.com/react@15/dist/react.min.js"></script>
<!-- React DOM -->
<script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
<!-- babel core-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.4.4/babel.min.js"></script>
</head>
<body>
<div id="machine-box"></div>
<script type="text/babel" src="components.js"></script>
</body>
</html>`
components.js
class MachineBox extends React.Component {
render(){
return ( <div>Hello From React </div> );
}
}
let target = document.getElementById('machine-box');
ReactDOM.render(
<MachineBox />, target
)
推荐答案
您的代码很好,但是您正在使用babel-standalone
的旧版本.
Your code is fine, you are using a really old version of babel-standalone
though.
// this
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.4.4/babel.min.js"></script>
// should be this
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.17.0/babel.min.js"></script>
和
<script type="text/babel" src="components.js"></script>
// should be
<script type="text/babel" src="components.js" data-presets="es2015,react"></script>
这篇关于使用Babel Standalone仅具有index.html和Component的单个React Component渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!