File tree Expand file tree Collapse file tree 2 files changed +47
-184
lines changed
Expand file tree Collapse file tree 2 files changed +47
-184
lines changed Original file line number Diff line number Diff line change 1+ // Class Component
2+ class Button extends React . Component {
3+ handleClick = ( ) => {
4+ this . props . onClickFunction ( this . props . incrementValue ) ;
5+ } ;
6+
7+ render ( ) {
8+ return (
9+ < button onClick = { this . handleClick } >
10+ +{ this . props . incrementValue }
11+ </ button >
12+ ) ;
13+ }
14+ }
15+
16+ // Functional Component
17+ const Result = ( props ) => {
18+ return (
19+ < div > { props . counter } </ div >
20+ ) ;
21+ } ;
22+
23+ // Parent Class Component
24+ class App extends React . Component {
25+ state = { counter : 0 } ;
26+
27+ incrementCounter = ( incrementValue ) => {
28+ this . setState ( ( prevState ) => ( {
29+ counter : prevState . counter + incrementValue
30+ } ) ) ;
31+ }
32+
33+ render ( ) {
34+ return (
35+ < div >
36+ < Button incrementValue = { 1 } onClickFunction = { this . incrementCounter } />
37+ < Button incrementValue = { 5 } onClickFunction = { this . incrementCounter } />
38+ < Button incrementValue = { 10 } onClickFunction = { this . incrementCounter } />
39+ < Button incrementValue = { 100 } onClickFunction = { this . incrementCounter } />
40+ < Result counter = { this . state . counter } />
41+ </ div >
42+ )
43+ }
44+ }
45+
46+ // React only renders one Component which is the reason for using a parent Component
47+ ReactDOM . render ( < App /> , mountNode ) ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments