亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

記React connect的幾種寫法(小結(jié))

 更新時間:2018年09月18日 11:11:27   作者:哥哦狗子  
這篇文章主要介紹了記React connect的幾種寫法(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])

連接 React 組件與 Redux store。

連接操作不會改變原來的組件類,反而返回一個新的已與 Redux store 連接的組件類。

參數(shù)

[mapStateToProps(state, [ownProps]): stateProps] (Function): 如果定義該參數(shù),組件將會監(jiān)聽 Redux store 的變化。任何時候,只要 Redux store 發(fā)生改變,mapStateToProps 函數(shù)就會被調(diào)用。該回調(diào)函數(shù)必須返回一個純對象,這個對象會與組件的 props 合并。如果你省略了這個參數(shù),你的組件將不會監(jiān)聽 Redux store。如果指定了該回調(diào)函數(shù)中的第二個參數(shù) ownProps,則該參數(shù)的值為傳遞到組件的 props,而且只要組件接收到新的 props,mapStateToProps 也會被調(diào)用。

[mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function): 如果傳遞的是一個對象,那么每個定義在該對象的函數(shù)都將被當作 Redux action creator,而且這個對象會與 Redux store 綁定在一起,其中所定義的方法名將作為屬性名,合并到組件的 props 中。如果傳遞的是一個函數(shù),該函數(shù)將接收一個 dispatch 函數(shù),然后由你來決定如何返回一個對象,這個對象通過 dispatch 函數(shù)與 action creator 以某種方式綁定在一起(提示:你也許會用到 Redux 的輔助函數(shù) bindActionCreators())。如果你省略這個 mapDispatchToProps 參數(shù),默認情況下,dispatch 會注入到你的組件 props 中。如果指定了該回調(diào)函數(shù)中第二個參數(shù) ownProps,該參數(shù)的值為傳遞到組件的 props,而且只要組件接收到新 props,mapDispatchToProps 也會被調(diào)用。

[mergeProps(stateProps, dispatchProps, ownProps): props] (Function): 如果指定了這個參數(shù),mapStateToProps() 與 mapDispatchToProps() 的執(zhí)行結(jié)果和組件自身的 props 將傳入到這個回調(diào)函數(shù)中。該回調(diào)函數(shù)返回的對象將作為 props 傳遞到被包裝的組件中。你也許可以用這個回調(diào)函數(shù),根據(jù)組件的 props 來篩選部分的 state 數(shù)據(jù),或者把 props 中的某個特定變量與 action creator 綁定在一起。如果你省略這個參數(shù),默認情況下返回 Object.assign({}, ownProps, stateProps, dispatchProps) 的結(jié)果。

[options] (Object) 如果指定這個參數(shù),可以定制 connector 的行為。

[pure = true] (Boolean): 如果為 true,connector 將執(zhí)行 shouldComponentUpdate 并且淺對比 mergeProps 的結(jié)果,避免不必要的更新,前提是當前組件是一個“純”組件,它不依賴于任何的輸入或 state 而只依賴于 props 和 Redux store 的 state。默認值為 true。

[withRef = false] (Boolean): 如果為 true,connector 會保存一個對被包裝組件實例的引用,該引用通過 getWrappedInstance() 方法獲得。默認值為 false

返回值

根據(jù)配置信息,返回一個注入了 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);

第二種

初次接觸,感覺有點繞,不太好理解,為什么點擊了,直接就派發(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ào)用一個方法,方法里再派發(fā)action
    這種直接點擊派發(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)自動有了dispatch的功能
const actionCreators={ addGunAction , removeGunAction , removeGunAsync}
export default connect(mapStateToProps,actionCreators)(App);

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深入了解響應(yīng)式React Native Echarts組件

    深入了解響應(yīng)式React Native Echarts組件

    近年來,隨著移動端對數(shù)據(jù)可視化的要求越來越高,通過 WebView 在移動端使用 Echarts 這樣功能強大的前端數(shù)據(jù)可視化庫,是解決問題的好辦法。下面就和小編來一起學習一下吧
    2019-05-05
  • 詳解在React里使用

    詳解在React里使用"Vuex"

    本篇文章主要介紹了詳解在React里使用"Vuex",小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • react ant-design Select組件下拉框map不顯示的解決

    react ant-design Select組件下拉框map不顯示的解決

    這篇文章主要介紹了react ant-design Select組件下拉框map不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • react-native彈窗封裝的方法

    react-native彈窗封裝的方法

    這篇文章主要為大家詳細介紹了react-native彈窗封裝的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 詳解React 和 Redux的關(guān)系

    詳解React 和 Redux的關(guān)系

    這篇文章主要為大家介紹了React 和 Redux的關(guān)系,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • React Hooks中模擬Vue生命周期函數(shù)的指南

    React Hooks中模擬Vue生命周期函數(shù)的指南

    React Hooks 提供了一種在函數(shù)組件中使用狀態(tài)和其他 React 特性的方式,而不需要編寫類組件,Vue 的生命周期函數(shù)和 React Hooks 之間有一定的對應(yīng)關(guān)系,本文給大家介紹了React Hooks中模擬Vue生命周期函數(shù)的指南,需要的朋友可以參考下
    2024-10-10
  • React簡單介紹

    React簡單介紹

    React 是一個用于構(gòu)建用戶界面的 JavaScript 庫,主要用于構(gòu)建 UI,而不是一個 MVC 框架,React 擁有較高的性能,代碼邏輯非常簡單,越來越多的人已開始關(guān)注和使用它
    2017-05-05
  • react 頁面加載完成后自動執(zhí)行標簽的點擊事件的兩種操作方法

    react 頁面加載完成后自動執(zhí)行標簽的點擊事件的兩種操作方法

    這篇文章主要介紹了react 頁面加載完成后自動執(zhí)行標簽的點擊事件,本文給大家分享兩種操作方法結(jié)合示例代碼給大家講解的非常詳細,需要的朋友可以參考下
    2022-12-12
  • React組件間通訊傳值實現(xiàn)詳解

    React組件間通訊傳值實現(xiàn)詳解

    這篇文章主要介紹了React組件間通訊傳值,react組件的通信屬于開發(fā)基礎(chǔ)知識,今天來梳理一下,當然rudex還按老規(guī)矩排除在外,如同上篇文章的hooks一樣,單獨梳理
    2022-11-11
  • React UI組件庫ant-design的介紹與使用

    React UI組件庫ant-design的介紹與使用

    Ant Design是阿里螞蟻金服團隊基于React開發(fā)的ui組件,主要用于中后臺系統(tǒng)的使用,這篇文章主要介紹了React UI組件庫ant-design的介紹與使用,需要的朋友可以參考下
    2023-12-12

最新評論