生活随笔
收集整理的這篇文章主要介紹了
2 中间件的使用、异步action的创建
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
react-redux是react插件 將所有組件分成兩大類:UI組件和容器組件 安裝npm install react-redux -S UI組件:
只負(fù)責(zé)UI的呈現(xiàn),不帶有任何業(yè)務(wù)邏輯 不使用this.state 所有數(shù)據(jù)都由this.props提供 不使用任何Redux的API,不需要使用store 容器組件:
負(fù)責(zé)管理數(shù)據(jù)和業(yè)務(wù)邏輯,不負(fù)責(zé)UI的呈現(xiàn) 帶有內(nèi)部狀態(tài) 使用Redux的API
組件結(jié)構(gòu)
用容器組件包裹UI組件 容器組件負(fù)責(zé)與外部的通信,將數(shù)據(jù)傳給UI組件 UI組件渲染出視圖 connect函數(shù)(來(lái)自react-redux插件):連接React組件與React store
關(guān)鍵字
App修改為UI組件
import ReactDOM
from 'react-dom'
import App
from './App'
import { createStore
} from 'redux'
import { counter
} from './redux/reducers'
import { Provider
} from 'react-redux' const store
= createStore ( counter
)
ReactDOM
. render ( < Provider store
= { store
} > < App
/ > < / Provider
> , document
. getElementById ( 'root' )
)
import React
, { Component
, createRef
} from 'react'
import { connect
} from 'react-redux'
import * as actions
from './redux/action'
class App extends Component { constructor ( props
) { super ( props
) this . selectRef
= createRef ( ) } compute = ( method
) => { const selectDom
= this . selectRef
. current
, selectVal
= Number ( selectDom
. value
) ; this . props
[ method
] ( selectVal
) } render ( ) { const { count
} = this . props
console . log ( '【app】' , this ) return ( < > < h1
> 數(shù)值:
{ count
} < / h1
> < select ref
= { this . selectRef
} > < option value
= "1" > 1 < / option
> < option value
= "2" > 2 < / option
> < option value
= "3" > 3 < / option
> < / select
> & nbsp
; < button onClick
= { ( ) => this . compute ( 'add' ) } > + < / button
> & nbsp
; < button onClick
= { ( ) => this . compute ( 'minus' ) } > - < / button
> & nbsp
; < button onClick
= { ( ) => this . compute ( 'add_odd' ) } > 奇數(shù)加
< / button
> & nbsp
; < button onClick
= { ( ) => this . compute ( 'add_delay' ) } > 延遲加
< / button
> < / > ) }
}
export default connect ( state
=> ( { count
: state
} ) , { ... actions
}
) ( App
)
connect方法接收2個(gè)參數(shù)
函數(shù)mapStateToProps,建立state對(duì)象到props對(duì)象的映射關(guān)系 (redux store里的state可以通過(guò)UI組件的props獲取) mapDispatchToProps,建立一個(gè)store.dispatch方法到props對(duì)象的方法 (redux里面action creators創(chuàng)建的函數(shù)可以通過(guò)props獲取)
使用中間件
reducer:純函數(shù),只承擔(dān)計(jì)算state的功能 view:與state意義對(duì)應(yīng) action:存放數(shù)據(jù)的對(duì)象,只能被別人操作? 同步:action發(fā)出后,reducer立即計(jì)算出state 異步:action發(fā)出后,過(guò)段時(shí)間再執(zhí)行reducer 中間件:一個(gè)函數(shù),對(duì)store.dispatch方法進(jìn)行改造,在發(fā)出action和執(zhí)行reducer兩步之間添加了其他功能
import ReactDOM
from 'react-dom'
import App
from './App'
import { createStore
, applyMiddleware
} from 'redux'
import thunk
from 'redux-thunk'
import { counter
} from './redux/reducers'
import { Provider
} from 'react-redux' const store
= createStore ( counter
, applyMiddleware ( thunk
) )
ReactDOM
. render ( < Provider store
= { store
} > < App
/ > < / Provider
> , document
. getElementById ( 'root' )
)
export function counter ( state
= 0 , action
) { const { type , data
} = action
switch ( type ) { case 'add' : return state
+ data
case 'minus' : return state
- data
case 'add_odd' : if ( data
% 2 !== 0 ) { return state
+ data
} default : return state
}
}
export function add ( param
) { return { type : 'add' , data
: param
}
}
export function minus ( param
) { return { type : 'minus' , data
: param
}
}
export function add_odd ( param
) { return { type : 'add_odd' , data
: param
}
}
export function add_delay ( param
) { return dispatch
=> { setTimeout ( ( ) => { dispatch ( add ( param
) ) } , 1000 ) }
}
創(chuàng)作挑戰(zhàn)賽 新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔 為你收集整理的2 中间件的使用、异步action的创建 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。