我正在尝试运行我的第一个React JSX文件,它可以工作!但是,我的JSX中没有这两个import语句:
import React from 'react';
import ReactDOM from 'react-dom';
Run Code Online (Sandbox Code Playgroud)
我以为我需要这两个导入,以便当JSX被编译为JS(由React.createElement)时,React组件将在范围内。但是,即使没有两个进口,它似乎也能正常工作。
没有进口商品怎么办?
这是我的代码:
script.jsx:
var Main = React.createClass({
getIntialState: function () {
return {
counter: 0
};
},
clickHandler: function () {
return {
counter: this.state.counter + 1
};
},
render: function () {
return (
<button onClick={this.clickHandler}>+2</button>
)
}
});
Run Code Online (Sandbox Code Playgroud)
package.json
{
"name": "reactjs",
"version": "1.0.0",
"description": "",
"main": "react.js",
"dependencies": {
"webpack": "^1.13.3"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",
"babel-loader": "^6.2.8",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"react": "^15.4.1",
"react-dom": "^15.4.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack --watch",
"build": "webpack -p"
},
"author": "",
"license": "ISC"
}
Run Code Online (Sandbox Code Playgroud)
HTML文件
<html>
<head>
<title>
!My first React JS Component!
</title>
</head>
<body>
<div id="root"></div>
<script src="react.js"></script>
<script src="output.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
module.exports = {
entry: "./app/script.jsx",
output: {
filename: "output.js"
},
module: {
loaders: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader'
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6954 次 |
| 最近记录: |