@@ -2,7 +2,8 @@ import React, { Component } from 'react';
2
2
// import './person/person.css';
3
3
// import './App.css';
4
4
import styles from './App.module.css' ;
5
- import Person from './person/Person.js' ;
5
+ import Person from './person/Person' ;
6
+ import ErrorBoundary from './ErrorBoundary/ErrorBoundary' ;
6
7
7
8
class App extends Component {
8
9
@@ -16,10 +17,6 @@ class App extends Component {
16
17
showPersons : false
17
18
}
18
19
19
- componentDidMount = ( ) => {
20
- console . log ( 'App Component Props' , this . props ) ;
21
- }
22
-
23
20
nimiChangedHandler = ( event , id ) => {
24
21
const personIndex = this . state . persons . findIndex ( p => {
25
22
return p . id === id ;
@@ -42,7 +39,7 @@ class App extends Component {
42
39
togglePersonsHandler = ( ) => {
43
40
const doesShow = this . state . showPersons ;
44
41
this . setState ( { showPersons : ! doesShow } ) ;
45
-
42
+ console . log ( 'ErrorBoundary' , ErrorBoundary )
46
43
}
47
44
48
45
deletePersonHandler = ( personIndex ) => {
@@ -56,50 +53,48 @@ class App extends Component {
56
53
render ( ) {
57
54
let persons = null ;
58
55
let btnClass = '' ;
59
-
60
56
if ( this . state . showPersons ) {
57
+
61
58
persons = (
62
59
< div >
63
- { this . state . persons . map ( ( person , index ) => {
64
- return (
65
- < Person
66
- click = { ( ) => this . deletePersonHandler ( index ) }
67
- nimi = { person . nimi }
68
- id = { person . id }
69
- key = { person . id }
70
- changed = { ( event ) => this . nimiChangedHandler ( event , person . id ) }
71
- />
72
- ) ;
73
- } ) }
60
+ { this . state . persons . map ( ( person , index ) => {
61
+ return < ErrorBoundary key = { person . id } >
62
+ < Person
63
+ click = { ( ) => this . deletePersonHandler ( index ) }
64
+ nimi = { person . nimi }
65
+ changed = { ( event ) => this . nimiChangedHandler ( event , person . id ) } />
66
+ </ ErrorBoundary >
67
+
68
+ } ) }
74
69
</ div >
75
70
) ;
76
- btnClass = styles . Red ;
71
+ btnClass = styles . Red ;
77
72
//btnClass = classes.Red;
78
- console . log ( 'btnClass:' , btnClass , 'styles.App:' , styles . App ) ;
73
+ console . log ( 'btnClass:' , btnClass , 'styles.App:' , styles . App ) ;
79
74
}
80
75
81
- const classes = [ ] ;
76
+ const assignedClasses = [ ] ;
82
77
if ( this . state . persons . length <= 2 ) {
83
- classes . push ( ' red' ) ;
84
- console . log ( classes ) ;
78
+ assignedClasses . push ( styles . red ) ;
79
+ console . log ( styles ) ;
85
80
}
86
81
if ( this . state . persons . length <= 1 ) {
87
- classes . push ( ' bold' ) ;
88
- console . log ( classes ) ;
82
+ assignedClasses . push ( styles . bold ) ;
83
+ console . log ( assignedClasses ) ;
89
84
}
90
85
91
86
92
87
console . log ( 'Print Styles' , styles ) ;
93
- console . log ( 'Classes ' , classes ) ;
88
+ console . log ( 'assignedClasses ' , assignedClasses ) ;
94
89
95
90
return (
96
91
< div className = { styles . App } >
97
92
< h1 > tekstiä</ h1 >
98
- < p className = { classes . join ( ' ' ) } > lisää tekstiä</ p >
93
+ < p className = { assignedClasses . join ( ' ' ) } > lisää tekstiä</ p >
99
94
100
95
< button
101
- className = { btnClass }
102
- onClick = { this . togglePersonsHandler } > ' Toggle Persons'
96
+ className = { btnClass }
97
+ onClick = { this . togglePersonsHandler } > Toggle Persons
103
98
</ button >
104
99
{ persons }
105
100
</ div >
0 commit comments