HOC in Depth

Higher-Order Components

https://reactjs.org/docs/higher-order-components.html

1. wrapper

props-proxy

https://codesandbox.io/s/react-hoc-in-depth-l8x1u?file=/src/components/hoc/props-proxy.jsx

import React, { Component } from "react";

function PropsProxyHOC(WrappedComponent) {
return class PPHOC extends Component {
constructor(props) {
super(props);
this.state = {
name: ""
};
this.onNameChange = this.onNameChange.bind(this);
} onNameChange(event) {
this.setState({
name: event.target.value
});
} render() {
const newProps = {
value: this.state.name,
onChange: this.onNameChange
};
// return <WrappedComponent {...this.props} {...newProps} />;
const mergedProps = {
...this.props,
...newProps
};
return <WrappedComponent {...mergedProps} />;
}
};
} export default PropsProxyHOC; // @PPHOC
// class Example extends React.Component {
// render() {
// return <input name="name" {...this.props.name}/>
// }
// }

2. enhancer

inheritance-inversion

https://codesandbox.io/s/react-hoc-in-depth-l8x1u?file=/src/components/hoc/inheritance-inversion.jsx

import React from "react";

function InheritanceInversionHOC(WrappedComponent) {
return class Enhancer extends WrappedComponent {
render() {
const elementsTree = super.render();
let newProps = {};
if (elementsTree && elementsTree.type === "input") {
newProps = {
value: "may the force be with you"
};
}
// const props = Object.assign({}, elementsTree.props, newProps);
const props = {
...elementsTree.props,
...newProps
};
const newElementsTree = React.cloneElement(
elementsTree,
props,
elementsTree.props.children
);
return newElementsTree;
}
};
} export default InheritanceInversionHOC;

zh-Hans

https://github.com/reactjs/reactjs.org/blob/master/content/docs/higher-order-components.md

https://github.com/reactjs/zh-hans.reactjs.org/blob/master/content/docs/higher-order-components.md

https://github.com/reactjs/zh-hans.reactjs.org/pull/170

blogs

https://www.aneureka.cn/2018/09/18/react-hoc-implementation/

https://juejin.im/post/5b7666b1e51d45560c1554a3

https://zhuanlan.zhihu.com/p/24776678



xgqfrms 2012-2020

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


HOC in Depth的更多相关文章

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

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

  2. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...

  5. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  6. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  7. LeetCode 104. Maximum Depth of Binary Tree

    Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...

  8. OpenCV2:Mat属性type,depth,step

    在OpenCV2中Mat类无疑使占据着核心地位的,前段时间初学OpenCV2时对Mat类有了个初步的了解,见OpenCV2:Mat初学.这几天试着用OpenCV2实现了图像缩小的两种算法:基于等间隔采 ...

  9. [Unity3D]深度相机 Depth Camera

    作为3D世界里最重要的窗口,摄像机的应用就显得很重要,毕竟在屏幕上看到的一切都得用摄像机矩阵变换得来的嘛.论坛上看到了一篇帖子讲非天空盒的背景做法,让我想起其实很多界面合成画面可以用摄像机之间的交互来 ...

随机推荐

  1. 两个报文是如何进行 TCP 分组传输

    16 | 如何理解TCP的"流"? https://time.geekbang.org/column/article/132443 TCP 是一种流式协议在前面的章节中,我们讲的都 ...

  2. 理解 async/await以及对Generator的优势

    async await 是用来解决异步的,async函数是Generator函数的语法糖使用关键字async来表示,在函数内部使用 await 来表示异步async函数返回一个 Promise 对象, ...

  3. Javascript 基础知识整理

    Javascript的作用 表单验证,减轻服务器压力 添加页面动画效果 动态更改页面内容 Ajax网络请求(异步加载数据) -它属于前端的核心,主要用来控制和重新调整DOM,通过修改DOM结构,从而达 ...

  4. java生成xls

    ------------------------------------------------------初始化xls操纵类-------- import java.io.File; import ...

  5. H3C交换机堆叠

    (1)      配置Device A# 将用作IRF物理端口的Ten-GigabitEthernet1/0/1-Ten-GigabitEthernet1/0/4的手工关闭.使用端口批量配置功能可以更 ...

  6. cassandra权威指南读书笔记--安全

    认证和授权driver,JMX和cassandra服务器支持SSL/TLS,cassandra节点间也支持SSL/TLS.密码认证器cassandra还支持自定义,可插拔的认证机制.默认的认证器:or ...

  7. Docker容器管理平台Rancher高可用部署——看这篇就够了

    记得刚接触Rancher时,看了官方文档云里雾里,跟着官网文档部署了高可用Rancher,发现不管怎么折腾都无法部署成功(相信已尝试的朋友也有类似的感觉),今天腾出空来写个总结,给看到的朋友留个参考( ...

  8. 设计模式(一)——Java单例模式(代码+源码分析)

    1)单例模式保证了 系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提高系统性能 2)当想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不 ...

  9. Pytest(17)运行未提交的git(pytest-picked)

    前言 我们每天写完自动化用例后都会提交到 git 仓库,随着用例的增多,为了保证仓库代码的干净,当有用例新增的时候,我们希望只运行新增的未提交 git 仓库的用例.pytest-picked 插件可以 ...

  10. Codeforces Global Round 8 C. Even Picture(构造)

    题目链接:https://codeforces.com/contest/1368/problem/C 题意 构造一个只含有灰.白块的网格,要求: 所有灰块为一个连通块 每个灰块与偶数个灰块相邻 恰有 ...