HOC in Depth
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的更多相关文章
- react系列(二)高阶组件-HOC
高阶组件 简单来说,高阶组件可以看做一个函数,且该函数接受一个组件作为参数,并返回一个新的组件. 我在之前的博客<闭包和类>中提到一个观点,面向对象的好处就在于,易于理解,方便维护和复用. ...
- [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 ...
- [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 ...
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...
- leetcode 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- 【leetcode】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- 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 ...
- OpenCV2:Mat属性type,depth,step
在OpenCV2中Mat类无疑使占据着核心地位的,前段时间初学OpenCV2时对Mat类有了个初步的了解,见OpenCV2:Mat初学.这几天试着用OpenCV2实现了图像缩小的两种算法:基于等间隔采 ...
- [Unity3D]深度相机 Depth Camera
作为3D世界里最重要的窗口,摄像机的应用就显得很重要,毕竟在屏幕上看到的一切都得用摄像机矩阵变换得来的嘛.论坛上看到了一篇帖子讲非天空盒的背景做法,让我想起其实很多界面合成画面可以用摄像机之间的交互来 ...
随机推荐
- session和cookie自动登录机制
cookie的存储 cookie是浏览器支持的一种本地存储方式.以dict,键值对方式存储. {"sessionkey": "123"} 浏览器会自动对于它进行 ...
- Java 类的加载与初始化
本文结构: 1.先看几道题 2.类的加载于初始化 (1)类的加载 (2)类的初始化 (a)会发生类的初始化的情况 (b)不会发生类的初始化的情况 首先看几道题. 解析可在看完讲解后再看 Demo1 p ...
- python 基础学习3 列表和元组 、字符串
作为小白,坚持每日写学习记录,是督促坚持学习的动力, 今天主要是学习 列表和元组,列表是可以修改的,元组是不可变的.列表和元组的索引都是从0开始 列表可以修改, 可以对列表进行赋值,修改移除等各种方法 ...
- Java 操作 HBase 教程
Java 操作 HBase 教程 一.简介 二.hbase-client 引入 三.连接操作 四.表操作 五.运行测试 相关博文原文地址: 博客园:美码师:HBase(2) Java 操作 HBase ...
- 深信服edr控制中心漏洞——验证码逻辑错误
验证码逻辑错误 文件:tool/log/l.php的第93行
- python--函数、参数、名称空间与作用域、匿名函数、内置函数、闭包
python函数 函数定义 def welcome(): print('hello world!!') welcome() #函数调用 ...运行结果 hello world!! 函数定义和编写原则: ...
- (4)Linux常用的运维平台和工具
运维工程师使用的运维平台和工具包括: Web服务器:apache.tomcat.nginx.lighttpd 监控:nagios.ganglia.cacti.zabbix 自动部署:ansible.s ...
- 痞子衡嵌入式:恩智浦MCUX SDK在GitHub上线了
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是恩智浦MCUX SDK 在GitHub上线一事. 其实在差不多一个月前(2021年1月6日),恩智浦MCUX SDK就在GitHub悄悄上 ...
- ness使用-漏扫
1.登录nessus后,会自动弹出目标输入弹框: 输入目标IP,可通过CIDR表示法(192.168.0.0/80),范围(192.168.0.1-192.168.0.255),或逗号分隔(192.1 ...
- shell脚本将字符串按指定分隔符切分成数组
Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下: array_name=(value1 value2 ... valuen) keyword=1,sdg,dd ...