記React connect的幾種寫法(小結(jié))
connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])
連接 React 組件與 Redux store。
連接操作不會(huì)改變?cè)瓉淼慕M件類,反而返回一個(gè)新的已與 Redux store 連接的組件類。
參數(shù)
[mapStateToProps(state, [ownProps]): stateProps] (Function): 如果定義該參數(shù),組件將會(huì)監(jiān)聽 Redux store 的變化。任何時(shí)候,只要 Redux store 發(fā)生改變,mapStateToProps 函數(shù)就會(huì)被調(diào)用。該回調(diào)函數(shù)必須返回一個(gè)純對(duì)象,這個(gè)對(duì)象會(huì)與組件的 props 合并。如果你省略了這個(gè)參數(shù),你的組件將不會(huì)監(jiān)聽 Redux store。如果指定了該回調(diào)函數(shù)中的第二個(gè)參數(shù) ownProps,則該參數(shù)的值為傳遞到組件的 props,而且只要組件接收到新的 props,mapStateToProps 也會(huì)被調(diào)用。
[mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function): 如果傳遞的是一個(gè)對(duì)象,那么每個(gè)定義在該對(duì)象的函數(shù)都將被當(dāng)作 Redux action creator,而且這個(gè)對(duì)象會(huì)與 Redux store 綁定在一起,其中所定義的方法名將作為屬性名,合并到組件的 props 中。如果傳遞的是一個(gè)函數(shù),該函數(shù)將接收一個(gè) dispatch 函數(shù),然后由你來決定如何返回一個(gè)對(duì)象,這個(gè)對(duì)象通過 dispatch 函數(shù)與 action creator 以某種方式綁定在一起(提示:你也許會(huì)用到 Redux 的輔助函數(shù) bindActionCreators())。如果你省略這個(gè) mapDispatchToProps 參數(shù),默認(rèn)情況下,dispatch 會(huì)注入到你的組件 props 中。如果指定了該回調(diào)函數(shù)中第二個(gè)參數(shù) ownProps,該參數(shù)的值為傳遞到組件的 props,而且只要組件接收到新 props,mapDispatchToProps 也會(huì)被調(diào)用。
[mergeProps(stateProps, dispatchProps, ownProps): props] (Function): 如果指定了這個(gè)參數(shù),mapStateToProps() 與 mapDispatchToProps() 的執(zhí)行結(jié)果和組件自身的 props 將傳入到這個(gè)回調(diào)函數(shù)中。該回調(diào)函數(shù)返回的對(duì)象將作為 props 傳遞到被包裝的組件中。你也許可以用這個(gè)回調(diào)函數(shù),根據(jù)組件的 props 來篩選部分的 state 數(shù)據(jù),或者把 props 中的某個(gè)特定變量與 action creator 綁定在一起。如果你省略這個(gè)參數(shù),默認(rèn)情況下返回 Object.assign({}, ownProps, stateProps, dispatchProps) 的結(jié)果。
[options] (Object) 如果指定這個(gè)參數(shù),可以定制 connector 的行為。
[pure = true] (Boolean): 如果為 true,connector 將執(zhí)行 shouldComponentUpdate 并且淺對(duì)比 mergeProps 的結(jié)果,避免不必要的更新,前提是當(dāng)前組件是一個(gè)“純”組件,它不依賴于任何的輸入或 state 而只依賴于 props 和 Redux store 的 state。默認(rèn)值為 true。
[withRef = false] (Boolean): 如果為 true,connector 會(huì)保存一個(gè)對(duì)被包裝組件實(shí)例的引用,該引用通過 getWrappedInstance() 方法獲得。默認(rèn)值為 false
返回值
根據(jù)配置信息,返回一個(gè)注入了 state 和 action creator 的 React 組件。
第一種
最普通,最常見,delllee和官網(wǎng)第寫法。
import React, { Component } from 'react';
import {connect} from 'react-redux';
import { Button } from 'antd-mobile';
import { addGunAction , removeGunAction , removeGunAsync}from './store/actionCreators'
class App extends Component {
render() {
console.log();
return (
<div className="App">
<p>{this.props.gun}</p>
<Button type="ghost" size="small" inline onClick={this.props.handeladd}>+</Button>
<Button type="ghost" size="small" inline onClick={this.props.handeljian}>-</Button>
<Button type="ghost" size="small" inline onClick={this.props.handelmanjian}>慢-</Button>
</div>
);
}
}
const mapStateToProps=(state)=>({
gun:state.gun
})
const mapDispachToProps=(dispatch)=>({
handeladd(){
dispatch(addGunAction())
},
handeljian(){
dispatch(removeGunAction())
},
handelmanjian(){
dispatch(removeGunAsync())
}
})
export default connect(mapStateToProps,mapDispachToProps)(App);
第二種
初次接觸,感覺有點(diǎn)繞,不太好理解,為什么點(diǎn)擊了,直接就派發(fā)action了?
import React, { Component } from 'react';
import {connect} from 'react-redux';
import { Button } from 'antd-mobile';
import { addGunAction , removeGunAction , removeGunAsync}from './store/actionCreators'
class App extends Component {
render() {
console.log();
return (
<div className="App">
<p>{this.props.gun}</p>
{/*不用像第一種那樣,點(diǎn)擊調(diào)用一個(gè)方法,方法里再派發(fā)action
這種直接點(diǎn)擊派發(fā)action就可以*/}
<Button type="ghost" size="small" inline onClick={this.props.addGunAction}>+</Button>
<Button type="ghost" size="small" inline onClick={this.props.removeGunAction}>-</Button>
<Button type="ghost" size="small" inline onClick={this.props.removeGunAsync}>慢-</Button>
</div>
);
}
}
const mapStateToProps=(state,ownProps)=>({
gun:state.gun
})
//這些action已經(jīng)自動(dòng)有了dispatch的功能
const actionCreators={ addGunAction , removeGunAction , removeGunAsync}
export default connect(mapStateToProps,actionCreators)(App);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React和Vue實(shí)現(xiàn)文件下載進(jìn)度條
本文主要介紹了React和Vue實(shí)現(xiàn)文件下載進(jìn)度條,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
高性能React開發(fā)React Server Components詳解
ReactServerComponents通過服務(wù)器端渲染、自動(dòng)代碼分割等技術(shù),實(shí)現(xiàn)了高性能的React開發(fā),它解決了客戶端數(shù)據(jù)請(qǐng)求鏈?zhǔn)窖舆t、敏感數(shù)據(jù)暴露風(fēng)險(xiǎn)等問題,提供了更好的用戶體驗(yàn)和安全性,本文介紹高性能React開發(fā)React Server Components詳解,感興趣的朋友一起看看吧2025-03-03
React Native中導(dǎo)航組件react-navigation跨tab路由處理詳解
這篇文章主要給大家介紹了關(guān)于React Native中導(dǎo)航組件react-navigation跨tab路由處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
React函數(shù)式組件Hook中的useEffect函數(shù)的詳細(xì)解析
useEffect是react v16.8新引入的特性。我們可以把useEffect hook看作是componentDidMount、componentDidUpdate、componentWillUnmounrt三個(gè)函數(shù)的組合2022-10-10
React + Node.js實(shí)現(xiàn)圖片上傳功能
最近筆者在開發(fā)個(gè)人博客的后臺(tái)管理系統(tǒng),里面用到了圖片上傳相關(guān)的功能,在這里記錄并分享一下,希望可以幫到大家,話不多說直接開始吧,感興趣的朋友可以參考下2024-01-01
React如何利用相對(duì)于根目錄進(jìn)行引用組件詳解
這篇文章主要給大家介紹了關(guān)于React如何使用相對(duì)于根目錄進(jìn)行引用組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10

