connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])
連接 React 組件與 Redux store。
連接操作不會改變原來的組件類,反而返回一個新的已與 Redux store 連接的組件類。
參數
[mapStateToProps(state, [ownProps]): stateProps] (Function): 如果定義該參數,組件將會監聽 Redux store 的變化。任何時候,只要 Redux store 發生改變,mapStateToProps 函數就會被調用。該回調函數必須返回一個純對象,這個對象會與組件的 props 合并。如果你省略了這個參數,你的組件將不會監聽 Redux store。如果指定了該回調函數中的第二個參數 ownProps,則該參數的值為傳遞到組件的 props,而且只要組件接收到新的 props,mapStateToProps 也會被調用。
[mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function): 如果傳遞的是一個對象,那么每個定義在該對象的函數都將被當作 Redux action creator,而且這個對象會與 Redux store 綁定在一起,其中所定義的方法名將作為屬性名,合并到組件的 props 中。如果傳遞的是一個函數,該函數將接收一個 dispatch 函數,然后由你來決定如何返回一個對象,這個對象通過 dispatch 函數與 action creator 以某種方式綁定在一起(提示:你也許會用到 Redux 的輔助函數 bindActionCreators())。如果你省略這個 mapDispatchToProps 參數,默認情況下,dispatch 會注入到你的組件 props 中。如果指定了該回調函數中第二個參數 ownProps,該參數的值為傳遞到組件的 props,而且只要組件接收到新 props,mapDispatchToProps 也會被調用。
[mergeProps(stateProps, dispatchProps, ownProps): props] (Function): 如果指定了這個參數,mapStateToProps() 與 mapDispatchToProps() 的執行結果和組件自身的 props 將傳入到這個回調函數中。該回調函數返回的對象將作為 props 傳遞到被包裝的組件中。你也許可以用這個回調函數,根據組件的 props 來篩選部分的 state 數據,或者把 props 中的某個特定變量與 action creator 綁定在一起。如果你省略這個參數,默認情況下返回 Object.assign({}, ownProps, stateProps, dispatchProps) 的結果。
[options] (Object) 如果指定這個參數,可以定制 connector 的行為。
[pure = true] (Boolean): 如果為 true,connector 將執行 shouldComponentUpdate 并且淺對比 mergeProps 的結果,避免不必要的更新,前提是當前組件是一個“純”組件,它不依賴于任何的輸入或 state 而只依賴于 props 和 Redux store 的 state。默認值為 true。
[withRef = false] (Boolean): 如果為 true,connector 會保存一個對被包裝組件實例的引用,該引用通過 getWrappedInstance() 方法獲得。默認值為 false
返回值
根據配置信息,返回一個注入了 state 和 action creator 的 React 組件。
第一種
最普通,最常見,delllee和官網第寫法。
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);
第二種
初次接觸,感覺有點繞,不太好理解,為什么點擊了,直接就派發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> {/*不用像第一種那樣,點擊調用一個方法,方法里再派發action 這種直接點擊派發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已經自動有了dispatch的功能const actionCreators={ addGunAction , removeGunAction , removeGunAsync}export default connect(mapStateToProps,actionCreators)(App);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答