-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.html
70 lines (59 loc) · 2.08 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>高阶组件 HOC</title>
<style>
.box {
margin: 20px;
padding: 20px;
border: 1px solid gray;
}
</style>
</head>
<body>
<div id="root"></div>
<script src="../libs/react.min.js"></script>
<script src="../libs/react-dom.min.js"></script>
<script src="../libs/babel.min.js"></script>
<script type="text/jsx">
/**
* 高阶组件的两种创建方式
* 1、属性代理(Props Proxy):类组件和函数组件都可以使用
* 2、反向继承(Inheritance Inversion, 缩写II):只用类组件可以使用
*/
/**
* 通过属性代理的方式向组件中注入 permission 属性
*/
function ComposeHOC(OriginComponent) {
const permission = 'edit permission from ComposeHOC'
return (props) => <OriginComponent {...props} permission={permission} />
}
/**
* 通过反向继承的方式向组件中注入 DOM 节点
*/
function iiHOC(OriginComponent) {
return class WrapperComponent extends OriginComponent {
render() {
return <div>
<h1>Title from iiHOC</h1>
{ super.render() }
</div>;
}
}
}
const ComponentA = (props) => <h2 className="box">ComponentA props permission: { props.permission }</h2>
class ComponentB extends React.Component {
render() {
return <h2 className="box">ComponentB</h2>;
}
}
// 使用高阶组件包裹 A、B 组件
const WrapperComponentA = ComposeHOC(ComponentA);
const WrapperComponentB = iiHOC(ComponentB);
const App = () => (<div><WrapperComponentA /><WrapperComponentB /></div>);
ReactDOM.render(<App />, root);
</script>
</body>
</html>