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世界里最重要的窗口,摄像机的应用就显得很重要,毕竟在屏幕上看到的一切都得用摄像机矩阵变换得来的嘛.论坛上看到了一篇帖子讲非天空盒的背景做法,让我想起其实很多界面合成画面可以用摄像机之间的交互来 ...
随机推荐
- 回归测试_百度百科 https://baike.baidu.com/item/%E5%9B%9E%E5%BD%92%E6%B5%8B%E8%AF%95
回归测试_百度百科https://baike.baidu.com/item/%E5%9B%9E%E5%BD%92%E6%B5%8B%E8%AF%95
- https://learnku.com/docs/go-blog/qihoo/6532 。 heap size went up to 69G, with maximum garbage collection (GC)
https://learnku.com/docs/go-blog/qihoo/6532 Use a Task Pool, a mechanism with a group of long-lived ...
- CSRF Laravel Cross Site Request Forgery protection¶
Laravel 使得防止应用 遭到跨站请求伪造攻击变得简单. Laravel 自动为每一个被应用管理的有效用户会话生成一个 CSRF "令牌",该令牌用于验证授权用 户和发起请求者 ...
- 前序遍历 排序 二叉搜索树 递归函数的数学定义 return 递归函数不能定义为内联函数 f(x0)由f(f(x0))决定
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- (二)SpringBoot应用运维脚本
SpringBoot应用运维脚本 一.获取PID 二.Kill命令 三.nohup命令 四.编写SpringBoot应用运维脚本 4.1全局变量 4.2编写核心方法 4.3Info方法 4.4stat ...
- sql,关键字使用
select instr('dds万','万',1) from dual --判断万关键字是否存在 select to_single_byte('9') from dual --全角数字转为半角数字 ...
- NodeMCU获取并解析心知天气信息
NodeMCU获取并解析心知天气信息 1 注册心知天气并获取私钥 打开心知天气网站,点击注册按钮 填写基本信息注册心知天气账号,登录注册所填写的邮箱点击链接进行账号激活,随后出现如下界面 点击登录按钮 ...
- docker(11)Dockerfile 中的COPY与ADD 命令
前言 Dockerfile 中提供了两个非常相似的命令 COPY 和 ADD,本文尝试解释这两个命令的基本功能,以及其异同点,然后总结其各自适合的应用场景. Build 上下文的概念 在使用 dock ...
- mysql查询太慢,我们如何进行性能优化?
老刘是即将找工作的研究生,自学大数据开发,一路走来,感慨颇深,网上大数据的资料良莠不齐,于是想写一份详细的大数据开发指南.这份指南把大数据的[基础知识][框架分析][源码理解]都用自己的话描述出来,让 ...
- C - C(换钱问题)
换钱问题: 给出n种钱,m个站点,现在有第 s种钱,身上有v 这么多: 下面 m行 站点有a,b两种钱,rab a->b的汇率,cab a-->b的手续费, 相反rba cba : 问在 ...