-
Notifications
You must be signed in to change notification settings - Fork 0
/
HOCyMixins.html
163 lines (135 loc) · 3.42 KB
/
HOCyMixins.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<main id="app"></main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.js"></script>
<script type="text/babel">
function Counter(WrappedComponent) {
class CounterHOC extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
this.handleClick = this.handleClick.bind(this);
}
getChildContext() {
return {
count: this.state.count,
};
}
handleClick() {
this.setState({
count: this.state.count + 1,
});
}
render() {
return (
<WrappedComponent
onClick={this.handleClick}
{...this.props}
/>
)
}
}
CounterHOC.childContextTypes = {
count: React.PropTypes.number,
};
return CounterHOC;
}
function Hola(props, context) {
return (
<h1 id="Title" onClick={props.onClick}>
Hola {props.name} {context.count}
</h1>
);
}
Hola.contextTypes = {
count: React.PropTypes.number
};
const HolaHOC = Counter(Hola);
class MiComponente extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
this.handleClick = this.handleClick.bind(this);
}
getChildContext() {
return {
count: this.state.count,
};
}
componentWillMount() {
console.log('el componente se va a montar');
}
componentDidMount() {
document.addEventListener('click', this.handleClick);
}
// componentWillReceiveProps(nextProps) {
// this.setState({
// count: this.state.count * 2,
// });
// }
shouldComponentUpdate(nextProps, nextState) {
if (nextState.count !== this.state.count) return true;
return false;
}
componentWillUpdate() {
console.log('el componente se va a actualizar');
}
componentDidUpdate() {
console.log('el componente se actualizó');
}
componentWillUnmount() {
document.removeEventListener('click', this.handleClick);
}
handleClick() {
this.setState({
count: this.state.count + 1,
});
}
handleMouseEnter(event) {
console.log(event.target);
}
render() {
return (
<Hola
name={this.props.name}
count={this.state.count}
onClick={this.handleClick}
onMouseEnter={this.handleMouseEnter}
/>
);
}
}
MiComponente.childContextTypes = {
count: React.PropTypes.number,
};
const mixin = {
handleClick() {
console.log('clickeado');
},
};
const Componente = React.createClass({
mixins: [
mixin,
],
render() {
return <div onClick={this.handleClick}>hola</div>;
}
})
ReactDOM.render(
<Componente />,
document.getElementById('app')
);
</script>
</body>
</html>