Skip to content

Commit d3f3240

Browse files
author
Yauhen
committed
Lesson 14: Portal
1 parent 7ef7747 commit d3f3240

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/14_portals/Lesson.jsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React, { Component } from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
class MyPortal extends Component {
5+
6+
el = document.createElement('div');
7+
8+
componentDidMount() {
9+
document.body.appendChild(this.el);
10+
}
11+
12+
componentWillUnmount() {
13+
document.body.removeChild(this.el);
14+
}
15+
16+
render() {
17+
return ReactDOM.createPortal(this.props.children, this.el);
18+
}
19+
}
20+
21+
class Lesson extends Component {
22+
state = {
23+
click: 0,
24+
}
25+
26+
handleClick = () => {
27+
this.setState(({ click }) => ({
28+
click: click + 1,
29+
}))
30+
}
31+
32+
render() {
33+
return (
34+
<div onClick={this.handleClick}>
35+
<p>Clicks: {this.state.click}</p>
36+
<span>Text</span>
37+
<MyPortal>
38+
<div>TEST PORTAL</div>
39+
<button>Click</button>
40+
</MyPortal>
41+
</div>
42+
);
43+
}
44+
}
45+
46+
export default Lesson;

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import App from './13_HOCs/Lesson.jsx';
3+
import App from './14_portals/Lesson.jsx';
44
import registerServiceWorker from './registerServiceWorker';
55

66
ReactDOM.render(<App />, document.getElementById('root'));

0 commit comments

Comments
 (0)