-
Notifications
You must be signed in to change notification settings - Fork 113
/
AutoUnmount.js
100 lines (75 loc) · 1.92 KB
/
AutoUnmount.js
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
import React from 'react';
import PropTypes from 'prop-types';
import {UnmountClosed} from '../../src';
class Test extends React.PureComponent {
static propTypes = {
onMount: PropTypes.func.isRequired,
onUnmount: PropTypes.func.isRequired
};
componentDidMount() {
const {onMount} = this.props;
onMount();
}
componentWillUnmount() {
const {onUnmount} = this.props;
onUnmount();
}
render() {
return <div>Test</div>;
}
}
export class AutoUnmount extends React.PureComponent {
static propTypes = {
isOpened: PropTypes.bool.isRequired
};
constructor(props) {
super(props);
const {isOpened} = this.props;
this.state = {isOpened};
this.counter = 0;
this.messages = [];
}
onRef = ref => {
this.ref = ref;
};
onMount = () => {
if (this.ref) {
this.messages.unshift(`${this.counter}. Mounted`);
this.messages = this.messages.slice(0, 5);
this.ref.innerHTML = this.messages.join('<br />');
this.counter = this.counter + 1;
}
};
onUnmount = () => {
if (this.ref) {
this.messages.unshift(`${this.counter}. Unmounted`);
this.messages = this.messages.slice(0, 5);
this.ref.innerHTML = this.messages.join('<br />');
this.counter = this.counter + 1;
}
};
onChange = ({target: {checked}}) => {
this.setState({isOpened: checked});
};
render() {
const {isOpened} = this.state;
return (
<div>
<div className="config">
<label className="label">
Opened:
<input
className="input"
type="checkbox"
checked={isOpened}
onChange={this.onChange} />
</label>
</div>
<UnmountClosed isOpened={isOpened}>
<Test onMount={this.onMount} onUnmount={this.onUnmount} />
</UnmountClosed>
<div className="log" ref={this.onRef} />
</div>
);
}
}