最近項目用到react,其實前年我就開始接觸react,時光匆匆,一直沒有時間整理下來(太懶啦)!現在再次用到,稱工做間隙,對全家桶作一次總結,項目源碼地址。廢話很少說,上碼。javascript
mkdir react-Buckets npm init
npm install --save-dev webpack
touch webpack.dev.config.js
var path=require('path'); module.exports={ // 入口文件指向src/index.js entry:path.join(__dirname,'src/index.js'), //打包後的文件到當前目錄下的dist文件夾,名爲bundle.js output:{ path:path.join(__dirname,'./dist'), filename:'bundle.js' } };
mkdir src && cd src touch index.js
document.getElementById('app').innerHTML='This is my React!';
webpack --config webpack.dev.config.js
效果如圖
html
touch ./dist/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="app"></div> <script type="text/javascript" src="./bundle.js"></script> </body> </html>
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build":"webpack --config webpack.dev.config.js" },
npm run build
平時你們在項目中無論用的vue仍是react,應該大多都開始用ES6或ES7的語法了吧。想必都瞭解若是想讓瀏覽器能夠直接識別,基本都會選用babel插件進行編譯轉換。下面爲你們一一介紹:vue
npm install --save-dev babel-core babel-loader babel-preset-es2015 babel-preset-react babel-preset-stage-0
安裝好後,添加配置文件java
touch .babelrc
打開文件,對babel進行配置,注:此處stage-0是包含stage-1,stage-2,stage-3react
{ "presets":[ "es2015", "react", "stage-0" ], "plugins":[] }
modle:{ rules:[{ test:/\.js$/, use:['babel-loader?cacheDirectory=true'], include:path.join(__dirname,'src') }] }
//使用ES6的箭頭函數 var babeltest=()=> console.log('This is Babel Test!'); babeltest();
npm install --save react react-dom
import React from 'react'; import ReactDom from 'react-dom'; ReactDom.render( <div>First React!</div>, document.getElementById('app') )
cd src mkdir components && cd components mkdir Hello && cd Hello touch Hello.js
import React, {Component} from 'react'; export default class Hello extends Component{ render(){ return( <div>Hello React!</div> ) } }
import React from 'react'; import ReactDom from 'react-dom'; import Hello from './components/Hello/Hello'; ReactDom.render( <Hello/>, document.getElementById('app'); )
npm install --save react-router-dom cd src mkdir router && touch router/router.js
import React from 'react'; import {BrowserRoter as Router,Route,Swith,Link} from 'react-router-dom'; import Home from '../pages/Home/Home'; import Page1 from '../pages/About/About'; const getRouter=()=>( <Router> <div> <ul> <li><Link to="/">首頁</Link></li> <li><Link to="/about">About</Link></li> </ul> <Switch> <Route exact path="/" componen={Home} /> <Route path="/about" component={About}/> </Switch> </div> </Router> ); export default getRouter;
cd src mkdir pages && cd pages mkdir Home && touch Home/Home.js mkdet About && touch About/About.js
import React,{Component} from 'react'; export default class Home extends Component{ render(){ return( <div> <h1>歡迎來到個人網站</h1> <p>這是一個首頁</p> </div> ) } }
import React,{Component} from 'react'; export default class About extends Component{ render(){ return( <div> <h2>關於咱們</h2> <p>自定義react全家桶</p> </div> ) } }
import React from 'react'; import ReactDom from 'react-dom'; import getRouter from './router/router'; ReactDom.render( getRouter(), document.getElementById('app') )
npm run build
咱們發現頁面是出來了,可是點擊切換不了路由,緣由是由於咱們須要配置一個web服務器來指向index.html,在這裏咱們來配置一個webpack-dev-server。webpack
webpack-dev-server是咱們作先後端分離時,常會用到的依賴,它是一個小型的靜態文件服務器,能夠爲webpack打包後生成的文件提供web服務器功能。git
npm install webpack-dev-server@2 --save-dev
devServer:{ //將服務器根目錄指向打包後的dist文件,默認是指向項目的根目錄 contentBase:path.join(__dirname,'./dist'); }
webpack-dev-server --config webpack.dev.config.js
打開http://localhost:8080
github
8080是默認端口,可更改配置。一樣,咱們把編譯命令優化下:web
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "webpack --config webpack.dev.config.js", "start": "webpack-dev-server --config webpack.dev.config.js --color --progress" },
proxy:{ "/api":"htpp://localhost:9000" }