1.前期准备:

1
2
全局下载工具:npm install -g create-react-ap
下载模板项目:npx create-react-app my-app

2.使用Visual Studio Code打开模板项目,如下图所示:

3.在Terminal中输入npm start,如下图所示:

4.编译器会自动打开项目的首页,此时显示的就是react的默认页面,如下图所示:

5.将默认生成出来的src文件夹下的全部文件删掉,这里需要重新创建如下图的文件或者文件夹,如下图所示:

添加文件夹:src/apisrc/assetsrc/componentssrc/configsrc/pagessrc/utilspublic/css

添加文件:public/css/reset.csssrc/App.jssrc/index.js

6.public/css/reset.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*! minireset.css v0.0.4 | MIT License | github.com/jgthms/minireset.css */
html,
body,
p,
ol,
ul,
li,
dl,
dt,
dd,
blockquote,
figure,
fieldset,
legend,
textarea,
pre,
iframe,
hr,
h1,
h2,
h3,
h4,
h5,
h6 {
margin: 0;
padding: 0;
}

h1,
h2,
h3,
h4,
h5,
h6 {
font-size: 100%;
font-weight: normal;
}

ul {
list-style: none;
}

button,
input,
select,
textarea {
margin: 0;
}

html {
box-sizing: border-box;
}

*, *:before, *:after {
box-sizing: inherit;
}

img,
embed,
iframe,
object,
video {
height: auto;
max-width: 100%;
}

audio {
max-width: 100%;
}

iframe {
border: 0;
}

table {
border-collapse: collapse;
border-spacing: 0;
}

td,
th {
padding: 0;
text-align: left;
}

html, body {
height: 100%;
width: 100%;
}

#root {
height: 100%;
width: 100%;
}

参考:https://github.com/jgthms/minireset.css/blob/master/minireset.css

7.public/index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
<link rel="stylesheet" href="/css/reset.css">
</head>
<body>
<div id="root"></div>
</body>
</html>

8.src/App.js

1
2
3
4
5
6
7
8
9
10
import React, {Component} from 'react'

/*
应用的根组件
*/
export default class App extends Component {
render () {
return <div>Hello world</div>
}
}

9.index.js

1
2
3
4
5
6
7
8
9
10
/*
入口js
*/
import React from 'react'
import ReactDOM from 'react-dom'

import App from './App'

// 将App组件标签渲染到index页面的div上
ReactDOM.render(<App />, document.getElementById('root'))

10.观察package.json是否有有react的对应的组件,如下图所示,保存上面的文件,服务会自动部署并显示最新的页面。

11.效果如下图所示: