React is a 由FacebookDevelopment open-sourceJavaScriptlibrary, 用于构建user界面. 它adoptscomponent化 Development方式, 让Development者可以easily构建 complex 单页application程序(SPA).
ReactDevelopment需要Node.js and npmenvironment, 首先需要installation它们:
node -v npm -v
such as果显示version号, 则表示installation成功.
Create React App is React官方推荐 project脚手架tool, 可以 fast 速creationReactapplication:
npx create-react-app my-react-app
其in my-react-app is project名称, 可以根据需要modify.
cd my-react-app
npm start
浏览器会自动打开 http://localhost:3000, 显示React默认页面.
usingCreate React Appcreation projectstructuresuch as under :
my-react-app/ ├── node_modules/ # project依赖 ├── public/ # 静态resource │ ├── favicon.ico │ ├── index.html # HTML模板 │ └── manifest.json ├── src/ # sourcescode │ ├── App.css # Appcomponent样式 │ ├── App.js # Appcomponent │ ├── App.test.js # Appcomponenttest │ ├── index.css # 全局样式 │ ├── index.js # application入口 │ ├── logo.svg # React logo │ └── reportWebVitals.js ├── .gitignore # Gitignorefile ├── package.json # projectconfiguration ├── package-lock.json └── README.md # project说明
public/index.html: HTML模板file, Reactapplication会挂载 to 这个file 根元素 on src/index.js: application入口file, 负责将Reactapplication渲染 to DOMinsrc/App.js: 根component, 整个application containerscomponentpackage.json: projectconfigurationfile, package含project依赖, 脚本commandsetc.让我们modify src/App.js file, creation一个 simple Reactcomponent:
import React from 'react';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Hello, React!</h1>
<p>
这 is 我 第一个Reactapplication.
</p>
</header>
</div>
);
}
export default App;
function App() 定义一个functioncomponentclassName 代替HTMLin class propertyexport default App exportcomponent, 供otherfileusingusing以 under commands启动Developmentserver, support热重载:
npm start
using以 under commands构建produceversion, 生成optimization after 静态file:
npm run build
构建completion after , 生成 file会放 in build Table of Contentsin.
using以 under commandsruntest:
npm test
usingCreate React Appcreation一个名 for my-first-react Reactproject, 并启动Developmentserver.
# creationproject npx create-react-app my-first-react # 进入projectTable of Contents cd my-first-react # 启动Developmentserver npm start
modify src/App.js file, 将页面标题改 for "我 Reactapplication", 并添加一个 simple 介绍段落.
import React from 'react';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>我 Reactapplication</h1>
<p>
这 is 我usingReactcreation 第一个application, 我将 in 这里LearningReact 各种features.
</p>
</header>
</div>
);
}
export default App;