当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 记React connect的几种写法(小结)

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

2018年09月23日  | 移动技术网IT编程  | 我要评论

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网