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);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 金池VS刘罡2009《双声情》[WAV+CUE]
- 冯奥迪《卄》[320K/MP3][67.08MB]
- 冯奥迪《卄》[FLAC/分轨][172.46MB]
- Merryland《Merryland》[320K/MP3][69.48MB]
- 苏芮.1991-停在我心里的温柔【福茂】【WAV+CUE】
- 苏慧伦.2020-面面Every.Side.of.Me【相信音乐】【WAV+CUE】
- 群星.1991-不一样的感觉REMIX【宝丽金】【WAV+CUE】
- 银霞.1983-《你那好冷的小手》珍藏纪念版[WAV分轨]
- 银霞.2002-《国语巨星专辑VOL.3》2CD台湾版[WAV+CUE]
- 金池刘罡《昨日情歌HQ》头版限量编号[低速原抓WAV+CUE]
- 段奥娟.2024-23·岁碎念【白米范】【FLAC分轨】
- 顺子.2001-昨日·唯一·更多·顺子自选辑【魔岩】【WAV+CUE】
- 无印良品.1999-私藏三首·原创相对论(EP)【滚石】【WAV+CUE】
- Merryland《Merryland》[FLAC/分轨][170.22MB]
- 庄锭欣 ELKIE《没有你的未来》[320K/MP3][15.78MB]