File tree 3 files changed +116
-0
lines changed
3 files changed +116
-0
lines changed Original file line number Diff line number Diff line change 3
3
/package-lock.json
4
4
5
5
/07-components.js
6
+ /08-components.js
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1 ">
6
+ < script src ="https://unpkg.com/react@16.12.0/umd/react.production.min.js "> </ script >
7
+ < script src ="https://unpkg.com/react-dom@16.12.0/umd/react-dom.production.min.js "> </ script >
8
+ </ head >
9
+ < body >
10
+ < p > class component</ p >
11
+ < div id ="root0 "> </ div >
12
+
13
+ < hr />
14
+
15
+ < p > class component with state</ p >
16
+ < div id ="root1 "> </ div >
17
+
18
+ < hr />
19
+
20
+ < p > class component with state and conditional</ p >
21
+ < div id ="root2 "> </ div >
22
+
23
+ < script src ="08-components.js "> </ script >
24
+ </ body >
25
+ </ html >
Original file line number Diff line number Diff line change
1
+
2
+ // npm install @babel /core @babel /preset-react @babel/cli
3
+ // npx babel --presets @babel/preset-react --no-comments -o 08-components.js 08-components.jsx
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+ // class component
13
+
14
+ class MyClassComponent extends React . Component {
15
+ render ( ) {
16
+ return (
17
+ < div >
18
+ < p > { this . props . name } ({ this . props . age } )</ p >
19
+ </ div >
20
+ ) ;
21
+ }
22
+ }
23
+ ReactDOM . render ( < MyClassComponent name = "John" age = { 45 } /> , document . getElementById ( "root0" ) ) ;
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+ // class component with state
33
+
34
+ class MyClassComponentWithState extends React . Component {
35
+ constructor ( props ) {
36
+ super ( props ) ;
37
+ this . state = {
38
+ date : "NOP" ,
39
+ } ;
40
+ }
41
+
42
+ render ( ) {
43
+ return (
44
+ < div >
45
+ < p > Click for the date: < button onClick = { ( ) => this . setState ( { date : ( new Date ( ) ) . toISOString ( ) } ) } > PUSH</ button > </ p >
46
+ < p > { this . props . message } { this . state . date } </ p >
47
+ </ div >
48
+ ) ;
49
+ }
50
+ }
51
+ ReactDOM . render ( < MyClassComponentWithState message = "The date is: " /> , document . getElementById ( "root1" ) ) ;
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+ // class component with state and conditional
61
+
62
+ class MyClassComponentWithToggle extends React . Component {
63
+ constructor ( props ) {
64
+ super ( props ) ;
65
+
66
+ this . state = {
67
+ showing : false ,
68
+ } ;
69
+
70
+ this . onClick = this . onClick . bind ( this ) ;
71
+ }
72
+
73
+ onClick ( ) {
74
+ this . setState ( state => {
75
+ return {
76
+ showing : ! state . showing
77
+ }
78
+ } ) ;
79
+ }
80
+
81
+ render ( ) {
82
+ return (
83
+ < div >
84
+ < p > { this . props . message } < button onClick = { this . onClick } > PUSH</ button > </ p >
85
+ { this . state . showing ? < MyClassComponentWithState message = "The date is: " /> : null }
86
+ </ div >
87
+ ) ;
88
+ }
89
+ }
90
+ ReactDOM . render ( < MyClassComponentWithToggle message = "Click to toggle: " /> , document . getElementById ( "root2" ) ) ;
You can’t perform that action at this time.
0 commit comments