在学习react-redux的时候,看到了修饰器这个新的属性,这个是es7的提案属性,很方便。于是我用@connect代替了connect(使用的时候需要配置,这里不赘述),省去了很多不必要的代码,但是我的view层和代码逻辑层是分开的,即view+hoc的模式:

先看封装的connect:

import {bindActionCreators} from "redux";
import {connect} from "react-redux";
import * as paperActions from "../redux/actions/index" export default connect(
state=>state,
dispatch=>bindActionCreators(paperActions,dispatch)
)

  在view页面中引用:

import React, {Component} from "react"
import connect from './../../containers/index'; @connect
export default class Test extends Component {
render() {
return (
<div>......</div>
)
}
}

  这个时候我们便已经取到了redux的action和state,那我所有的逻辑代码在hoc文件里面:

import React, {Component} from "react";

const getDisplayName = component => component.displayName || component.name || "Component";

const hoc = WrappedComponent => {
return class extends Component {
static displayName = `HOC(${getDisplayName(WrappedComponent)})`; // 构造
constructor(props) {
super(props);
} componentDidMount() {
console.log(this.props);
} render() {
const props = {
...this.props, };
return <WrappedComponent {...props} />
}
}
}; export default hoc

  此时打印出来“this.props”是得不到state和action的,然后我再hoc里面尝试了各种方法,却一直在报错:

Leading decorators must be attached to a class declaration

  很明显,修饰器只能放在类的前面,于是谷歌了许多资料,发现其实hoc就是一个纯函数,可以当修饰器来使用,于是:

import React, {Component} from "react"
import connect from './../../containers/index';
import hoc from "./hoc"
@connect
@hoc
export default class Test extends Component {
render() {
return (
<div>......</div>
)
}
}

 在hoc中就可以使用redux里面的actions和state了

react中的hoc和修饰器@connect结合使用的更多相关文章

  1. 19.Decorator修饰器

    Decorator 修饰器 类的修饰 许多面向对象的语言都有修饰器(Decorator)函数,用来修改类的行为.目前,有一个提案将这项功能,引入了 ECMAScript. @testable clas ...

  2. 修饰器Decorator

    类的修饰 许多面向对象的语言都有修饰器(Decorator)函数,用来修改类的行为.目前,有一个提案将这项功能,引入了 ECMAScript. @testable class MyTestableCl ...

  3. python函数修饰器(decorator)

    python语言本身具有丰富的功能和表达语法,其中修饰器是一个非常有用的功能.在设计模式中,decorator能够在无需直接使用子类的方式来动态地修正一个函数,类或者类的方法的功能.当你希望在不修改函 ...

  4. Decorator [ˈdekəreɪtə(r)] 修饰器/装饰器 -- 装饰模式

    装饰模式 -- 原先没有,后期添加的属性和方法 修饰器(Decorator)是一个函数,用来修饰类的行为.这是ES7的一个提案,目前Babel转码器已经支持. 需要先安装一个插件: npm insta ...

  5. ES2017中的修饰器Decorator

    前面的话 修饰器(Decorator)是一个函数,用来修改类的行为.本文将详细介绍ES2017中的修饰器Decorator 概述 ES2017 引入了这项功能,目前 Babel 转码器已经支持Deco ...

  6. mobx中使用class语法或decorator修饰器时报错

    之前课程中老师用的babel的版本比较低,我在学习时安装的babel版本较高,因此每当使用class语法或decorator修饰器时都会出现一些报错的情况! ❌ ERROR in ./src/inde ...

  7. es6 Decorator修饰器

    1.类的修饰: 修饰器(Decorator)函数,用来修改类的行为.修饰器是一个对类进行处理的函数.修饰器函数的第一个参数,就是所要修饰的目标类. @testable class MyTestable ...

  8. js基石之---es7的decorator修饰器

    es7的decorator修饰器 装饰器(Decorator)是一种与类(class)相关的语法,用来注释或修改类和类方法. decorator就是给类添加或修改类的变量与方法的. 装饰器是一种函数, ...

  9. Python修饰器的函数式编程

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...

随机推荐

  1. Generalization and Equilibrium in Generative Adversarial Nets

    Paper link: https://arxiv.org/abs/1703.00573 Blog link: http://www.offconvex.org/2017/03/30/GANs2/ G ...

  2. Android开发之旅(二)服务生命周期和广播接收者生命周期

    引言 应用程序组件有一个生命周期——一开始Android实例化他们响应意图,直到结束实例被销毁.在这期间,他们有时候处于激活状态,有时候处于非激活状态:对于活动,对用户有时候可见,有时候不可见.组件生 ...

  3. e833. 获得JTabbedPane中的卡片

    This example retrieves all the tabs in a tabbed pane: // To create a tabbed pane, see e828 创建JTabbed ...

  4. e770. 确定按钮租中已选的单选按钮

    When you ask a button group for the currently selected radio button, it returns the selected radio b ...

  5. C#提高-------------------Assembly和Module的使用-------反射内涵

    转 :C#反射技术概念作用和要点 反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以 ...

  6. 初步了解学习将传统单机应用改造成Dubbo服务的过程

    Dubbo作为RPC框架,实现的效果就是调用远程的方法就像在本地调用一样.如何做到呢?就是本地有对远程方法的描述,包括方法名.参数.返回值,在Dubbo中是远程和本地使用同样的接口:然后呢,要有对网络 ...

  7. win10 禁用Defender

    cmd reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /v "Dis ...

  8. http_build_query用法

    http_build_query (PHP 5) http_build_query -- 生成 url-encoded 之后的请求字符串描述string http_build_query ( arra ...

  9. MATLAB错误:下标索引必须是正整数类型或者逻辑类型

    背景: Matlab R2015b 问题: 在运行BP算法时出现错误: 下标索引必须是正整数类型或者逻辑类型 output( i , class( i )  ) = 1 ; 解决办法: 根目录下运行, ...

  10. linux 下启动tomca慢问题

    编辑文件vim /etc/profile 后面加入一句:export JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom" 设置立 ...