起步
- 可以直接到React中文网直接进行下载。
- 可以使用npm进行安装
npm安装方法
平台:win10
安装nodejs
直接去官网下载最新版,一直next进行安装
设置淘宝镜像
npm config set registry https://registry.npm.taobao.org
安装React全局包
npm install babel -g
npm install webpack -g
npm install webpack-dev-server -g
创建根目录
自己创建个英文目录,cd到这个目录下,使用 npm init 进行初始化。
F:\ReactProject>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (ReactProject) MyFirstApp
Sorry, name can no longer contain capital letters.
name: (ReactProject) my_first_app
version: (1.0.0)
description: 我的第一个React应用
entry point: (index.js)
test command:
git repository:
keywords:
author: 张华焱
license: (ISC)
About to write to F:\ReactProject\package.json:
{
"name": "my_first_app",
"version": "1.0.0",
"description": "我的第一个React应用",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "张华焱",
"license": "ISC"
}
Is this ok? (yes)
添加依赖包及插件
--save 命令用于将包添加至 package.json 文件
npm install react --save
npm install react-dom --save
同时我们也要安装一些 babel 插件
npm install babel-core
npm install babel-loader
npm install babel-preset-react
npm install babel-preset-es2015
创建必要文件
- index.html
- App.jsx
- main.js
- webpack.config.js
设置编译器,服务器,载入器
打开 webpack.config.js 文件添加以下代码:
var config = {
entry: './main.js',
output: {
path:'./',
filename: 'index.js',
},
devServer: {
inline: true,
port: 7777
},
module: {
loaders: [ {
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}]
}
}
module.exports = config;
- entry: 指定打包的入口文件 main.js。
- output:配置打包结果,path定义了输出的文件夹,filename则定义了打包结果文件的名称。
- devServer:设置服务器端口号为 7777,端口后你可以自己设定 。
- module:定义了对模块的处理逻辑,这里可以用loaders定义了一系列的加载器,以及一些正则。当需要加载的文件匹配test的正则时,就会调用后面的loader对文件进行处理,这正是webpack强大的原因。
现在打开 package.json 文件,找到 "scripts" 中的 "test" "echo \"Error: no test specified\" && exit 1" 使用以下代码替换:
"start": "webpack-dev-server --hot"
现在我们可以使用 npm start 命令来启动服务。--hot 命令会在文件变化后重新载入,这样我们就不需要在代码修改后重新刷新浏览器就能看到变化
编辑index.html
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>我的第一React应用</title>
</head>
<body>
<div id = "app"></div>
<script src = "index.js"></script>
</body>
</html>
App.jsx 和 main.js
App.jsx代码
import React from 'react';
class App extends React.Component {
render() {
return (
<div>
Hello World!!!<br />
我的第一个React应用
</div>
);
}
}
export default App;
我们需要引入组件并将其渲染到根元素 App 上,这样我们才可以在浏览器上看到它。
main.js 文件代码
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
ReactDOM.render(<App />, document.getElementById('app'))
运行服务
npm start
如果运行过程中发生错误
Module not found: Error: Cannot resolve module 'webpack/hot/dev-server'
就把webpack配置到当前目录下,不要配置在全局中
npm install babel
npm install webpack
npm install webpack-dev-server
重新运行就可以了