总结了点React,咱也不敢说
生活随笔
收集整理的這篇文章主要介紹了
总结了点React,咱也不敢说
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
React總結(jié)
1. 搭環(huán)境
此處默認(rèn)各位都是有那么一點(diǎn)前端開發(fā)經(jīng)驗的,所以Node自行安裝。
1.1 腳手架
- 官方推薦 => create-react-app
1.2 起項目
1.2.1 package.json
// setting "scripts": {"start": "react-scripts start","build": "react-scripts build","test": "react-scripts test","eject": "react-scripts eject" } 復(fù)制代碼1.2.2 start
// start $ npm run start 復(fù)制代碼1.2.3 打包
// start $ npm run build 復(fù)制代碼2. 項目結(jié)構(gòu)
|public |----|favicon.ico |----|index.html |dist |src |----|common | |pages | |statics | |----|img | | |iconfont | |store | |----|index.js | | |reducer.js | | |actionTypes.js | | |actionCreators.js | |App.js | |index.js |package.json |README.md 復(fù)制代碼3. 入口文件
-
/src/index
-
指定渲染的文件以及渲染的文件插入的DOM節(jié)點(diǎn)(document.getElementById('root'))。
4. 渲染的入口文件
-
/src/App.js
-
路由/全局樣式/派發(fā)store……
4.1 路由
- 依賴包 => react-router-dom
4.1.1 install
$ npm i react-router-dom -S 復(fù)制代碼4.1.2 路由表
import { BrowserRouter, Route } from "react-router-dom";<BrowserRouter><Route path="/" exact component={Home} /><Route path="/detail" exact component={Detail} /> </BrowserRouter> 復(fù)制代碼-
exact精確匹配path路徑。
-
component表示當(dāng)前路徑加載組件。
4.1.3 路由傳參
4.1.3.1 通配符
- 刷新頁面參數(shù)不會丟失,但很丑
4.1.3.2 query
- 好看但會丟失參數(shù)
4.1.3.3 state
- 同query
4.1.4 路由跳轉(zhuǎn)
4.1.4.1 Link
<Line to="/path"></Link> 復(fù)制代碼4.1.4.2 history.push
this.props.history.push({ pathname:' /user', ……}) 復(fù)制代碼4.2 樣式
-
依賴包 => styled-components
-
將頁面中的標(biāo)簽等用js封裝成樣式的組件。
4.2.1 全局樣式
// 1. 導(dǎo)出GlobalStyle import styled, { createGlobalStyle } from "styled-components";export const GlobalStyle = createGlobalStyle` html {width: 100%;height: 100%; } `;// 2. 在渲染入口文件中添加 <Provider><div style={providerStyle}><GlobalStyle /><FontGlobal /><BrowserRouter><RouteStyle><Route path="/" exact component={Home} /><Route path="/detail" exact component={Detail} /></RouteStyle></BrowserRouter></div> </Provider> 復(fù)制代碼4.2.2 局部樣式
import styled from "styled-components";export const Img = styled.img.attrs({src: xxx })`width: 100px;height: 100px; `;// 導(dǎo)入使用 <Img/> 復(fù)制代碼4.3 派發(fā)store
import { Provicer } from "react-redux"; import store from "./store"<Provicer store={store}>// 組件內(nèi)都能接收到store </Provicer> 復(fù)制代碼4.3.1 組件內(nèi)接收
import { connect } "react-redux";class Header extends Component {render() {return (<div onClick={this.props.handleDispach}>Hello</div>this.props.login? <div>Logout</div>: <div>Login</div>)} }const mapStateToProps = (state) => ({// 映射store里面的值login: state.get("login") });const mapDispatchToProps = (dispatch) => {return {// 接受掛載在props上的方法 handleDispach() {// TODOdispatch(action);}} };export default connect(mapStateToProps, mapDispatchToProps)(Heacer); 復(fù)制代碼5. redux
- 依賴包 => redux/react-redux/redux-thunk/immutable/redux-immutable
5.1 redux
- 用于構(gòu)建store實例
5.2 react-redux
-
用于派發(fā)store,在頁面中映射state數(shù)據(jù)等。
-
如上4.3.
5.3 redux-thunk
-
同過redux的中間件使用thunk。
-
用于在actionCreator.js中返回一個函數(shù)。
-
異步數(shù)據(jù)的處理。
5.4 immutable
- 用于將數(shù)據(jù)結(jié)構(gòu)變成immutable形式,強(qiáng)制修改會報錯。
5.5 redux-immutable
- 用于將多個reducer合并。
6. 動畫
-
依賴包 => react-transition-group
-
具體上GitHub
7. 生命周期函數(shù)
- 官網(wǎng)
轉(zhuǎn)載于:https://juejin.im/post/5cb738f3e51d456e5a0728e2
總結(jié)
以上是生活随笔為你收集整理的总结了点React,咱也不敢说的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spark常用函数比较
- 下一篇: postgresql日常操作命令