File tree Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import ReactDOM from 'react-dom' ;
3
- import App from './13_HOCs /Lesson.jsx' ;
3
+ import App from './14_portals /Lesson.jsx' ;
4
4
import registerServiceWorker from './registerServiceWorker' ;
5
5
6
6
ReactDOM . render ( < App /> , document . getElementById ( 'root' ) ) ;
You can’t perform that action at this time.
0 commit comments