Skip to content

Commit 5239e67

Browse files
committed
ErrorBoundary broken
1 parent ac356ad commit 5239e67

File tree

7 files changed

+35
-40
lines changed

7 files changed

+35
-40
lines changed

.DS_Store

0 Bytes
Binary file not shown.

firstapp/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
.env.development.local
1818
.env.test.local
1919
.env.production.local
20+
/node_modules
2021

2122
npm-debug.log*
2223
yarn-debug.log*

firstapp/src/App.js

+24-29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import React, { Component } from 'react';
22
// import './person/person.css';
33
// import './App.css';
44
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';
67

78
class App extends Component {
89

@@ -16,10 +17,6 @@ class App extends Component {
1617
showPersons: false
1718
}
1819

19-
componentDidMount = () => {
20-
console.log('App Component Props', this.props);
21-
}
22-
2320
nimiChangedHandler = (event, id) => {
2421
const personIndex = this.state.persons.findIndex(p => {
2522
return p.id === id;
@@ -42,7 +39,7 @@ class App extends Component {
4239
togglePersonsHandler = () => {
4340
const doesShow = this.state.showPersons;
4441
this.setState({showPersons: !doesShow});
45-
42+
console.log('ErrorBoundary',ErrorBoundary)
4643
}
4744

4845
deletePersonHandler = (personIndex) => {
@@ -56,50 +53,48 @@ class App extends Component {
5653
render() {
5754
let persons = null;
5855
let btnClass = '';
59-
6056
if (this.state.showPersons) {
57+
6158
persons = (
6259
<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+
})}
7469
</div>
7570
);
76-
btnClass= styles.Red;
71+
btnClass= styles.Red;
7772
//btnClass = classes.Red;
78-
console.log('btnClass:',btnClass,'styles.App:',styles.App);
73+
console.log('btnClass:',btnClass,'styles.App:',styles.App);
7974
}
8075

81-
const classes = [];
76+
const assignedClasses = [];
8277
if (this.state.persons.length <= 2) {
83-
classes.push('red');
84-
console.log(classes);
78+
assignedClasses.push(styles.red);
79+
console.log(styles);
8580
}
8681
if (this.state.persons.length <= 1) {
87-
classes.push('bold');
88-
console.log(classes);
82+
assignedClasses.push(styles.bold);
83+
console.log( assignedClasses);
8984
}
9085

9186

9287
console.log('Print Styles', styles);
93-
console.log('Classes', classes);
88+
console.log('assignedClasses', assignedClasses);
9489

9590
return (
9691
<div className={styles.App}>
9792
<h1>tekstiä</h1>
98-
<p className={classes.join(' ')}>lisää tekstiä</p>
93+
<p className={assignedClasses.join(' ')}>lisää tekstiä</p>
9994

10095
<button
101-
className={btnClass}
102-
onClick={this.togglePersonsHandler}> 'Toggle Persons'
96+
className={btnClass}
97+
onClick={this.togglePersonsHandler}> Toggle Persons
10398
</button>
10499
{persons}
105100
</div>

firstapp/src/ErrorBoundary/ErrorBoundary.js

Whitespace-only changes.

firstapp/src/person/Person.css

-8
This file was deleted.

firstapp/src/person/Person.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import React from 'react';
22

3-
import './Person.css';
3+
import styles from './Person.module.css';
4+
45

56
const person = ( props ) => {
7+
const rnd = Math.random();
8+
9+
if ( rnd > 0.7 ) {
10+
throw new Error ( 'Something went wrong' );
11+
}
12+
613
return (
7-
<div className="Person">
8-
<p onClick={props.click}>I am {props.nimi} and I am {props.id} years old!</p>
14+
<div className={styles.person}>
15+
<p onClick={props.click}>I am {props.nimi} and I am {Math.random()} years old!</p>
916
<p>{props.children}</p>
1017
<input type="text" onChange={props.changed} value={props.nimi} />
1118
</div>

src/.DS_Store

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)