高阶类 & HOC & anonymous class extends

js 匿名 class extends / mix-ins / 多继承

高阶函数 HOF, 接收一个 function 返回一个新的 function

高阶组件 HOC, 接收一个 component 返回一个新的 component

高阶类 HOC , 接收一个 class 返回一个新的 class

Mix-ins

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Mix-ins

  1. // 匿名 class extends
  2. let calculatorMixin = Base => class extends Base {
  3. calc() { }
  4. };
  5. // 匿名 class extends
  6. let randomizerMixin = Base => class extends Base {
  7. randomize() { }
  8. };
  1. class Foo { }
  2. class Bar extends calculatorMixin(randomizerMixin(Foo)) {
  3. //
  4. }

Mixins Considered Harmful

https://reactjs.org/blog/2016/07/13/mixins-considered-harmful.html

匿名 class extends


  1. // This function takes a component...
  2. function withSubscription(WrappedComponent, selectData) {
  3. // ...and returns another component...
  4. // return class HOC extends React.Component {
  5. // 匿名 class extends
  6. return class extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.handleChange = this.handleChange.bind(this);
  10. this.state = {
  11. data: selectData(DataSource, props)
  12. };
  13. }
  14. componentDidMount() {
  15. // ... that takes care of the subscription...
  16. DataSource.addChangeListener(this.handleChange);
  17. }
  18. componentWillUnmount() {
  19. DataSource.removeChangeListener(this.handleChange);
  20. }
  21. handleChange() {
  22. this.setState({
  23. data: selectData(DataSource, this.props)
  24. });
  25. }
  26. render() {
  27. // ... and renders the wrapped component with the fresh data!
  28. // Notice that we pass through any additional props
  29. return <WrappedComponent data={this.state.data} {...this.props} />;
  30. }
  31. };
  32. }

Debugging

Wrap the Display Name for Easy Debugging


  1. function withSubscription(WrappedComponent) {
  2. // 命名 class extends
  3. class WithSubscription extends React.Component {/* ... */}
  4. // 获取 组件名
  5. WithSubscription.displayName = `WithSubscription(${getDisplayName(WrappedComponent)})`;
  6. return WithSubscription;
  7. }
  8. function getDisplayName(WrappedComponent) {
  9. return WrappedComponent.displayName || WrappedComponent.name || 'Component';
  10. }

  1. import React, { Component } from 'react';
  2. // 获取组件名
  3. function getDisplayName(WrappedComponent) {
  4. return WrappedComponent.displayName || WrappedComponent.name || 'HOC Component';
  5. }
  6. // HOC 1. 接收一个组件作为参数
  7. function HOC(WrappedComponent, props) {
  8. // do something
  9. const name = WrappedComponent.name || "hoc";
  10. HOC.displayName = `HOC_${getDisplayName(WrappedComponent)}`;
  11. // HOC 2. 返回一个新组件
  12. // 匿名 class extends (Anonymous)
  13. // return class extends Component {
  14. // 命名 class extends ()
  15. return class HOC extends Component {
  16. // constructor(props) {
  17. // super();
  18. // }
  19. render() {
  20. return (
  21. <>
  22. <WrappedComponent props={props} data={name}/>
  23. </>
  24. )
  25. }
  26. }
  27. }
  28. export default HOC;

refs

https://reactjs.org/docs/higher-order-components.html#use-hocs-for-cross-cutting-concerns

https://reactjs.org/docs/higher-order-components.html#convention-wrap-the-display-name-for-easy-debugging



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


高阶类 & HOC & anonymous class extends的更多相关文章

  1. 高阶函数 HOF & 高阶组件 HOC

    高阶函数 HOF & 高阶组件 HOC 高阶类 js HOC 高阶函数 HOF 函数作为参数 函数作为返回值 "use strict"; /** * * @author x ...

  2. react系列(二)高阶组件-HOC

    高阶组件 简单来说,高阶组件可以看做一个函数,且该函数接受一个组件作为参数,并返回一个新的组件. 我在之前的博客<闭包和类>中提到一个观点,面向对象的好处就在于,易于理解,方便维护和复用. ...

  3. React: 高阶组件(HOC)

    一.简介 如我们所知,JavaScript有高阶函数这么一个概念,高阶函数本身是一个函数,它会接收或者返回一个函数,进而对该函数进行操作.其实,在React中同样地有高阶组件这么一个东西,称为HOC, ...

  4. 高阶函数HOF和高阶组件HOC(Higher Order Func/Comp)

    一.什么是高阶函数(组件),作用是什么? 子类使用父类的方法可以通过继承的方式实现,那无关联组件通信(redux).父类使用子类方法(反向继承)呢 为了解决类(函数)功能交叉/功能复用等问题,通过传入 ...

  5. 高阶组件 HOC

    一. A higher-order component (HOC) is an advanced technique in React for reusing component logic. a h ...

  6. 聊聊React高阶组件(Higher-Order Components)

    使用 react已经有不短的时间了,最近看到关于 react高阶组件的一篇文章,看了之后顿时眼前一亮,对于我这种还在新手村晃荡.一切朝着打怪升级看齐的小喽啰来说,像这种难度不是太高同时门槛也不是那么低 ...

  7. React 精要面试题讲解(五) 高阶组件真解

    说明与目录 在学习本章内容之前,最好是具备react中'插槽(children)'及'组合与继承' 这两点的知识积累. 详情请参照React 精要面试题讲解(四) 组合与继承不得不说的秘密. 哦不好意 ...

  8. 函数式编程与React高阶组件

    相信不少看过一些框架或者是类库的人都有印象,一个函数叫什么creator或者是什么什么createToFuntion,总是接收一个函数,来返回另一个函数.这是一个高阶函数,它可以接收函数可以当参数,也 ...

  9. 关于react16.4——高阶组件(HOC)

    高阶组件是react中用于重用组件逻辑的高级技术.可以说是一种模式.具体来说呢,高阶组件是一个函数,它接收一个组件并返回一个新的组件. 就像这样, const EnhancedComponent = ...

随机推荐

  1. 转 16 jmeter中的监听器以及测试结果分析

    16 jmeter中的监听器以及测试结果分析   常用监听器 断言结果.查看结果树.聚合报告.Summary Report.用表格查看结果.图形结果.aggregate graph等 指标分析 -Sa ...

  2. GStreamer环境搭建篇

    GStreamer是一套强大的多媒体中间件系统,跟FFmpeg功能类似. 各个Linux发行版(Ubuntu,fedora),大都集成了GStreamer相关工具,而作为软件层次结构最上层的播放器,几 ...

  3. XA Transactions

    XA Transactions XA is a two-phase commit protocol that is natively supported by many databases and t ...

  4. 初始TypeScript

    什么是TypeScript? TypeScript是拥有类型系统的JavaScript的超集,可以编译成纯JavaScript: 1.类型检查:TS会在编译代码时进行严格的静态类型检查,这意味着可以在 ...

  5. How Interfaces Work in Go

    research!rsc: Go Data Structures: Interfaces https://research.swtch.com/interfaces How Interfaces Wo ...

  6. SQLyog破解30天到期

    开始--运行中输入regedit.找到regedit.exe 文件   点击regedit.exe...就把注册表编辑器打开了   我们需要找到记录软件使用实现的数据...找到HKEY-CURRENT ...

  7. LIS的优化

    二分优化 在求一个最长不上升自序列中,显然其结尾元素越小,越有利于接其他元素,对答案的贡献也就可能会更高 那么我们可以用low[i]去存长度为i的LIS结尾元素的最小值 因此我们只要维护low数组 对 ...

  8. redis防止重复提交

    public interface DistributedLock { boolean getLock(String var1, String var2, int var3);//加锁 void unL ...

  9. P4570 [BJWC2011]元素 (线性基)

    题意:n个石头 每个石头有a,b两个属性 要求选出一些石头使得没有一个子集的a属性xor和为0 且b属性和最大 题解:线性基例题了.. 好像需要理解一些性质 1.原序列里任一数都可有由线性基xor得到 ...

  10. P3803 [模板] 多项式乘法 (FFT)

    Rt 注意len要为2的幂 #include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); inli ...