react-redux中mapDispatchToProps的几种方式

作者: MJ 分类: react 发布时间: 2019-03-16 22:28

react-redux中使用mapDispatchToProps的几种方式

1:第一种形式

const mapDispatchToProps = (dispatch) => {
  return {
      jia:() => {
          dispatch(increment())
      },
      jian:() => {
          dispatch(decrement())
      }
  }
}
export default connect(mapStateToProps,mapDispatchToProps)(App);

2:第二种形式

import {bindActionCreators} from 'redux';
const mapDispatchToProps = (dispatch) => {
  return {
     jia:bindActionCreators(increment,dispatch),
     jian:bindActionCreators(decrement,dispatch)
   }
}
export default connect(mapStateToProps,mapDispatchToProps)(App);

3:第三种形式

import {bindActionCreators} from 'redux';
const mapDispatchToProps = (dispatch) => {
  return bindActionCreators({increment,decrement},dispatch)
}
export default connect(mapStateToProps,mapDispatchToProps)(App);

4:第四种形式

import {bindActionCreators} from 'redux';
import * as types from './actions'
const mapDispatchToProps = (dispatch) => {
  return bindActionCreators(types,dispatch)
}
export default connect(mapStateToProps,mapDispatchToProps)(App);

5:第五种形式

import {increment,decrement} from './actions'
export default connect(mapStateToProps,{increment,decrement})(App);

欢迎关注小程序,感谢您的支持!

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注