React connect 写法 记React connect的几种写法(小结)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

React connect 写法 记React connect的几种写法(小结)

哥哦狗子   2021-03-30 我要评论

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);

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们