美文网首页
用装饰器的方法替换react-redux

用装饰器的方法替换react-redux

作者: 俗人彭jin | 来源:发表于2018-12-29 14:15 被阅读0次

用babel-plugin-transform-decorators-legacy . babel-plugin-transform-decorators插件替换connect

https://www.jianshu.com/p/2dca3fb17139 这是以前的写法

// 获得数据
const mapStateToProps = (state) => {
    return {num: state}
}
// 获得方法,方法是上面倒入的 //将action的所有方法绑定到props上
const mapDispatchToProps = {addGum, addGumAsync}
// connect 第一个是获得数据源,第二个是获得方法源
Test1 = connect(mapStateToProps, mapDispatchToProps)(Test1)
export default Test1

下载上面的插件之后需要在webpack里面配置插件

 "babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
      [
        "import",
        {
          "libraryName": "antd-mobile",
          "style": "css"
        }
      ],
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
        }
      ],
      [
        "@babel/plugin-proposal-class-properties",
        {
          "loose": true
        }
      ]
    ]
  }
"import",
        {
          "libraryName": "antd-mobile",
          "style": "css"
        }这里是配置antd-mobile按需加载

,
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
        }
      ],
      [
        "@babel/plugin-proposal-class-properties",
        {
          "loose": true
        }
      ]  这里是支持上面插件的
    

上面条件满足之后

import React, {Component} from 'react'
import {Button} from 'antd-mobile'
import {connect} from 'react-redux'
import {addGum, addGumAsync} from "../index.redux";
@connect(
// 你需要啥属性放到state里面
    state=>({num: state}),
    {
        addGum,addGumAsync //你需要啥方法放到这里
    }
)
class Test1 extends Component {
    render() {
        let num = this.props.num
        return (
            <div className="test01">
                <p>{num}</p>
                <Button onClick={this.props.addGum}>add</Button>
                <Button onClick={this.props.addGumAsync}>延迟增加</Button>
            </div>
        )
    }
}
export default Test1

相关文章

  • 用装饰器的方法替换react-redux

    用babel-plugin-transform-decorators-legacy . babel-plugin-...

  • python装饰器,生成器,迭代器

    装饰器 运用了闭包的原理通过一个@符号将装饰器置于其要装饰的方法的上方可以多个装饰器装饰同一个方法 装饰器可以用可...

  • 装饰器

    装饰器通常把函数替换成另一个函数 装饰器的一大特性,能把被装饰的函数替换成其他函数。特性二,装饰器在加载模块时立即执行

  • Py进阶|装饰器

    函数装饰器是函数替换的过程——被装饰的函数被替换成另一个东西。 以上涉及到两个函数: 装饰器函数,用于修饰其他函数...

  • typescript 五种装饰器

    装饰器类型 装饰器的类型有:类装饰器、访问器装饰器、属性装饰器、方法装饰器、参数装饰器,但是没有函数装饰器(fun...

  • 如何理解Python装饰器

    装饰器本质 装饰器本质上是用一个新的函数替换原先的函数. 本质上是如下语句 使用wraps保存原函数的name和d...

  • 类里方法的装饰器

    类的方法的装饰器 方法的装饰器的执行时间也是在类定义之后,立即对类的方法进行装饰修改 方法的装饰器接受3个参数 ...

  • python装饰器的用法

    这一篇我们来讲讲装饰器的用法 装饰器有什么作用呢,见名知意,装饰用的。如果我们写了一个方法,要为这个方法增加一个功...

  • TypeScript装饰器

    前言 装饰器分类 类装饰器 属性装饰器 方法装饰器 参数装饰器需要在tsconfig.json中启用experim...

  • TS装饰器初体验,用装饰器管理koa接口

    typescript中的装饰器有很多种,比如类装饰器、方法装饰器、属性装饰器等等,先看看装饰器的定义吧,下面以类装...

网友评论

      本文标题:用装饰器的方法替换react-redux

      本文链接:https://www.haomeiwen.com/subject/eogulqtx.html