Create React App

Facebook開源了React前端框架(MIT Licence),也同時提供了React腳手架 - create-react-appcss

create-react-app遵循約定優於配置(Coc)的原則,默認配置、項目結構等,讓Web開發人員可以快速上手React。html

搭建create-react-app:前端

1. 安裝nodenode

  下載連接, 選擇適當版本;react

  node -v  # 檢查node版本webpack

  npm -v  # 檢查npm版本git

2. 全局安裝create-react-app腳手架github

  npm install -g create-react-appweb

3. 建立react appajax

  create-react-app react-app (該步會比較慢,與網絡相關)

  

4. 查看README.md

5. 運行react app

  npm start

6. 瀏覽http://localhost:3000/

以上是利用create-react-app來建立react app的步驟。

下面對README.md解讀~

(1)當create-react-app升級後,能夠只升級react-scripts。步驟可遵循這裏

(2)`public/index.html`和`src/index.js`這兩個文件必須存在。Webpack會處理src文件夾裏的內容,js,css,html等須要置於src中

(3)已配置好的script:

  1. npm start    -> 開發模式下運行於http://localhost:3000

  2. npm test     -> js測試

  3. npm run build  -> build react app用於生產環境並生成到build文件夾下

(4)支持Debugging的文本編輯器:VSCode,WebStorm

  1. VSCode + Chrome Debugger Extension

    在launch.json中添加以下,(端口號由配置決定)

{
  "version": "0.2.0",
  "configurations": [{
    "name": "Chrome",
    "type": "chrome",
    "request": "launch",
    "url": "http://localhost:3000",
    "webRoot": "${workspaceRoot}/src",
    "sourceMapPathOverrides": {
      "webpack:///src/*": "${webRoot}/*"
    }
  }]
}

    啓動調試,

  2. WebStorm + JetBrains IDE Support(略) 

(5)git commit前代碼格式化

  1. 運行 npm install --save husky lint-staged prettier

  2. package.json中scripts屬性添加 "precommit": "lint-staged"

  3. package.json中添加屬性lint-staged    

"lint-staged":{
    "src/**/*.{js,jsx,json,css}": [
      "prettier --single-quote --write",
      "git add"
    ]
  }

(6)組件,多使用export default component, import component from './component'

(7)建議各個組件有本身的一套css,儘可能不復用(Generally, we recommend that you don’t reuse the same CSS classes across different components.)

(8)添加圖像、字體、文件

  webpack在build過程當中會根據靜態資源內容hash,以後重命名靜態資源,避免瀏覽器緩存問題。能夠相似import組件的方式import靜態資源。  

import React from 'react';
import logo from './logo.png'; // Tell Webpack this JS file uses this image

console.log(logo); // /logo.84287d09.png

function Header() {
  // Import result is the URL of your image
  return <img src={logo} alt="Logo" />;
}

export default Header;
.Logo {
  background-image: url(./logo.png);
}

 (9)`public`文件夾

  該文件夾中的內容不會被webpack處理,且相互之間訪問需使用前綴PUBLIC_URL。  

<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">

(10)使用全局變量

const $ = window.$;

(11)使用Bootstrap

npm install --save react-bootstrap bootstrap@3

react-bootstrap中並無包含bootstrap的css,所以也須要install bootstrap

src/index.js中,import bootstrap.css

import 'bootstrap/dist/css/bootstrap.css'; import 'bootstrap/dist/css/bootstrap-theme.css'; // Put any other imports below so that CSS from your
// components takes precedence over default styles.

接下來在組件中import bootstrap的組件

import { Navbar, Jumbotron, Button } from 'react-bootstrap';

(12)靜態類型檢查工具 - Flow

create-react-app集成步驟:  

1. Run `npm install --save flow-bin` (or `yarn add flow-bin`).
2. Add `"flow": "flow"` to the `scripts` section of your `package.json`.
3. Run `npm run flow init` (or `yarn flow init`) to create a [`.flowconfig` file] in the root directory.
4. Add `// @flow` to any files you want to type check (for example, to `src/App.js`).

then run `npm run flow` (or `yarn flow`) to check the files for type errors.

(13)環境變量

1. 在create-react-app中,除了內置的環境變量,如NODE_ENV、PUBLIC_URL外,其他環境變量須要用REACT_APP_做爲前綴

2. 在定義了環境變量後,須要從新build才能生效

3. js中訪問環境變量,加前綴process.env.

REACT_APP_SECRET_CODE -> process.env.REACT_APP_SECRET_CODE

4. 在.env文件中,定義環境變量

在項目根目錄下建立.env文件,定義環境變量

此外,還有其餘env文件,好比.env.local,優先級以下:

* `npm start`: `.env.development.local` > `.env.development` > `.env.local` > `.env`
* `npm run build`: `.env.production.local` > `.env.production` > `.env.local` > `.env` 
* `npm test`: `.env.test.local` > `.env.test` > `.env`

 ***********

(14)經過ajax獲取數據

 React並無規定如何獲取數據,但一般可使用全局函數fetch()發送ajax請求,解析返回的Promise對象。

(15)開發環境中與後臺API整合

目前在各類框架下的前端開發,都會使用webpack, express等做爲相似server,佔據端口,提供服務,可是後臺api的開發與測試也一樣須要佔據端口。雖然端口各有所用,可是習 慣傳統開發的人,難免以爲這樣作徹底是資源的浪費,畢竟傳統開發方式只用一個端口就ok了。

 從react-scripts@0.2.3版本開始,create-react-app提供了proxy字段,用於設置請求後臺api時所用到的host和端口。

package.json

"proxy": "http://localhost:4000",

注意點:

  1. proxy字段僅在開發過程當中起做用(npm start)

  2. 請求頭中accept=text/html將被忽略掉

  3. 能夠具體配置proxy增長靈活性

同時,能夠配置websocket和https的代理

配置https代理:(當api server提供https服務時)

Windows: 

set HTTPS=true&&npm start

Linux & macOS

HTTPS=true npm start

(16)build後包大小分析

使用source-map-explorer ,步驟以下:

1. install source-map-explorer

npm install --save source-map-explorer

2. 在package.json中的scripts中添加:

"analyze": "source-map-explorer build/static/js/main.*",

3. 運行命令

npm run build
npm run analyze

 (17)Advanced Configuration

Done! 

相關文章
相關標籤/搜索