1
+ import React from 'react' ;
2
+ import styles from './index.module.less' ;
3
+
4
+ class Template extends React . Component {
5
+ // init data
6
+ constructor ( props ) {
7
+ super ( props ) ;
8
+
9
+ this . state = {
10
+ // data that will be used/changed in render function,
11
+ // here to initiate them
12
+ // you can see "state" as an Object.
13
+
14
+ isButtonClicked : false ,
15
+ count : 0 ,
16
+
17
+ }
18
+ // Every function that interfaces with UI and data used
19
+ // in this class needs to bind like this:
20
+ this . someFunction1 = this . someFunction1 . bind ( this ) ;
21
+ this . someFunction2 = this . someFunction2 . bind ( this ) ;
22
+ this . handleOnClickBtn = this . handleOnClickBtn . bind ( this ) ;
23
+ }
24
+
25
+ someFunction1 ( ) {
26
+ return
27
+ }
28
+
29
+ someFunction2 ( ) {
30
+ return
31
+ }
32
+
33
+ handleOnClickBtn ( ) {
34
+ console . log ( 'count =' , this . state . count ) ;
35
+ let currentCount = this . state . count ;
36
+ // Every time you wants to modify the variables you declared in this.state,
37
+ // You need to use this function.
38
+ this . setState ( {
39
+ isButtonClicked : true ,
40
+ count : currentCount + 1 ,
41
+ } ) ;
42
+ }
43
+
44
+ componentDidMount ( ) {
45
+ // this is a function that will automatically be called the first time the component renders
46
+ }
47
+
48
+ componentDidUpdate ( ) {
49
+ // this is a function that will automatically be called each time the component renders
50
+ }
51
+
52
+ render ( ) {
53
+ return (
54
+ < div className = { styles . template } >
55
+ < p > this is a template component!</ p >
56
+ < span > this is a link to < a href = "/" > homepage</ a > </ span >
57
+ < div >
58
+ < div > Here is a count button</ div >
59
+ < div >
60
+ < button onClick = { this . handleOnClickBtn } >
61
+ {
62
+ this . state . isButtonClicked ?
63
+ "clicked!" :"click me"
64
+ }
65
+ </ button >
66
+ </ div >
67
+ < div > You have clicked { this . state . count } time(s)!</ div >
68
+ </ div >
69
+ </ div >
70
+ ) ;
71
+ }
72
+
73
+ }
74
+
75
+ export default Template ;
0 commit comments