ReactBasicsconcepts and Environment Setup

1. ReactBasicsconcepts

1.1 what is React?

React is a 由FacebookDevelopment open-sourceJavaScriptlibrary, 用于构建user界面. 它adoptscomponent化 Development方式, 让Development者可以easily构建 complex 单页application程序(SPA).

1.2 React core思想

2. Environment Setup

2.1 installationNode.js and npm

ReactDevelopment需要Node.js and npmenvironment, 首先需要installation它们:

  1. 访问 Node.js官网 under 载并installationNode.js
  2. installationcompletion after , 打开终端, run以 under commandsverificationinstallation:
node -v
npm -v

such as果显示version号, 则表示installation成功.

2.2 usingCreate React Appcreationproject

Create React App is React官方推荐 project脚手架tool, 可以 fast 速creationReactapplication:

  1. 打开终端, run以 under commandscreationReactproject:
npx create-react-app my-react-app

其in my-react-app is project名称, 可以根据需要modify.

  1. projectcreationcompletion after , 进入projectTable of Contents:
cd my-react-app
  1. 启动Developmentserver:
npm start

浏览器会自动打开 http://localhost:3000, 显示React默认页面.

3. Reactprojectstructure

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说明

3.1 corefile说明

4. 第一个Reactcomponent

让我们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;

4.1 component解析

5. run and 构建project

5.1 Development模式

using以 under commands启动Developmentserver, support热重载:

npm start

5.2 构建produceversion

using以 under commands构建produceversion, 生成optimization after 静态file:

npm run build

构建completion after , 生成 file会放 in build Table of Contentsin.

5.3 testproject

using以 under commandsruntest:

npm test

6. 练习

练习1: creationReactproject

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

练习2: modifyAppcomponent

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;