1,安裝NODE,html
2,安裝webpacknode
1 npm install -g webpack
3,配置淘寶鏡像
react
使用淘寶定製的 cnpm (gzip 壓縮支持) 命令行工具代替默認的 npmwebpack
1 npm install -g cnpm --registry=https://registry.npm.taobao.org 2 npm config set registry https://registry.npm.taobao.org
4,安裝create-react-appgit
1 cnpm install -g create-react-app
1 create-react-app my-project//建立項目 2 cd my-project/
6,本地服務啓動github
1 npm run start//啓動本地server用於開發
React Router 是一個基於 React 之上的強大路由庫,它可讓你嚮應用中快速地添加視圖和數據流,同時保持頁面與 URL 間的同步。react-routerweb
1 npm install react-router@4.3.1 --save
1 render(){ 2 return ( 3 <Router> 4 <div> 5 <ul> 6 <li><Link to="/home">首頁</Link></li> 7 <li><Link to="/other">其餘頁</Link></li> 8 </ul> 9 <Route path="/home" component={Home}/> 10 <Route path="/other" component={Other}/> 11 </div> 12 </Router> 13 ) 14 }
路由支持嵌套,代碼以下:npm
1 const Root = () => ( 2 <div> 3 <Switch> 4 <Route 5 path="/" 6 render={props => ( 7 <App> 8 <Switch> 9 <Route path="/" exact component={Home} /> 10 <Route path="/home" component={Home} /> 11 <Route path="/test" component={Test} /> 12 <Route path="/message/:id/:TestId" component={Message} /> 13 {/*路由不正確時,默認跳回home頁面*/} 14 <Route render={() => <Redirect to="/" />} /> 15 </Switch> 16 </App> 17 )} 18 /> 19 </Switch> 20 </div> 21 ); 22 23 export default Root;
React是單向數據流,因此有些狀況下單單依賴React自身沒法實現組件之間的通訊,故此須要reduxjson
須要安裝Redux相關依賴,因爲不一樣版本之間可能存在兼容性問題,因此安裝的時候最好制定版本:redux
1 npm install redux@3.7.2 --save 2 npm install redux-thunk@2.1.0 --save 3 npm install react-redux@5.0.6 --save
而後就能夠在項目中引入redux了,能夠像以下方式組織目錄結構:
關於請求數據有不少種方式,這裏採用的是fetch
1 npm install fetch --save
能夠簡單封裝一下,以下:
1 import 'whatwg-fetch' 2 import loggerService from './logger' 3 4 let notAuthorizedCounter = 0; 5 let fetchService = { 6 fetch: (url, method, header, body) => { 7 if (!header) { 8 header = {} 9 } 10 11 return fetchService[method.toLowerCase()](url, header, body).catch(function(exception) { 12 loggerService.log('fetchService failed:', exception); 13 14 // token過時,從新獲取token併發起請求 15 if (exception.code === '401' || exception.code === '403') { 16 notAuthorizedCounter++; 17 // 最多重試3次 18 if (notAuthorizedCounter > 2) { 19 notAuthorizedCounter = 0; 20 loggerService.warn("401 or 403 received. Max attemps reached."); 21 return; 22 } else { 23 return fetchService.fetch(url, method, header, body); 24 } 25 } 26 }); 27 }, 28 get: (url, header) => { 29 return fetch(url, { 30 method: 'GET', 31 headers: header 32 }).then((response) => { 33 return response.json(); 34 }); 35 }, 36 post: (url, header, body) => { 37 header['Content-Type'] = 'application/json'; 38 return fetch(url, { 39 method: 'POST', 40 headers: header, 41 body: JSON.stringify(body) 42 }).then((response) => { 43 return response.json(); 44 }); 45 } 46 }; 47 export default fetchService;
基於React的UI組件在這裏推薦兩個,一個是螞蟻金服的Ant Design;另一個是Material-UI。
兩個都很不錯,這裏使用的是Ant Design。
1 npm install antd --save
請注意 react >= 16.6.3和react-dom >= 16.6.3 是對等依賴
1 npm install @material-ui/core
採用的是經常使用的react-intl
1 npm install react-intl --save
首先對項目進行打包。
1 npm run build
經過如下命令能夠在本地環境運行打包後的項目。
1 serve -s build
PropTypes 的使用
JavaScript 是弱類型語言,因此請儘可能聲明 propTypes 對 props 進行校驗,以減小沒必要要的問題。
1 import React from 'react'; 2 import PropTypes from 'prop-types' 3 4 class PropTypesList extends React.Component { 5 static propTypes = {//校驗 6 title: PropTypes.string, 7 }; 8 static defaultProps = {//設置默認值 9 title:"PropTypes 的應用實例"//添加默認值 10 }; 11 constructor(props) { 12 super(props); 13 this.state = { 14 15 } 16 } 17 render() { 18 const { title} = this.props 19 return ( 20 <div> 21 <p>{title}</p> 22 </div> 23 ); 24 } 25 } 26 27 export default PropTypesList ;
內置的 prop type 有: