Skip to content

Commit 74d75ce

Browse files
committed
Minor Update
Split App into components
1 parent ba87663 commit 74d75ce

File tree

4 files changed

+75
-52
lines changed

4 files changed

+75
-52
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.Cockpit {
2+
text-align: center;
3+
}
4+
5+
.red {
6+
color: red;
7+
}
8+
9+
.bold {
10+
font-weight: bold;
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import styled from 'styled-components';
3+
4+
const StyledButton = styled.button`
5+
background-color: ${props => props.alt ? 'red' : 'green'};
6+
color: white;
7+
font: inherit;
8+
border: 1px solid blue;
9+
padding: 8px;
10+
cursor: pointer;
11+
12+
&:hover {
13+
background-color: ${props => props.alt ? 'salmon' : 'lightgreen'};
14+
color: black;
15+
}
16+
`;
17+
18+
const cockpit = (props) => {
19+
const classes = [];
20+
if (props.persons.length <= 2) {
21+
classes.push('red'); // classes = ['red']
22+
}
23+
if (props.persons.length <= 1) {
24+
classes.push('bold'); // classes = ['red', 'bold']
25+
}
26+
27+
return(
28+
<div>
29+
<h1>Hi, I'm a React App</h1>
30+
<p className={classes.join(' ')}>This is really working!</p>
31+
<StyledButton
32+
alt={props.showPersons}
33+
onClick={props.clicked}>
34+
Toggle Persons
35+
</StyledButton>
36+
</div>
37+
)}
38+
39+
export default cockpit;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import Person from './Person/Person';
3+
4+
const persons = (props) => props.persons.map((person, index) => {
5+
return (
6+
<Person
7+
click={() => props.clicked(index)}
8+
name={person.name}
9+
age={person.age}
10+
key={person.id}
11+
changed={event => props.changed(event, person.id)}/>);
12+
});
13+
14+
export default persons;

Class Notes/my-app/src/containers/App.js

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
import React, { Component } from 'react';
2-
import styled from 'styled-components';
32

43
import './App.css';
5-
import Person from '../components/Persons/Person/Person';
6-
7-
const StyledButton = styled.button`
8-
background-color: ${props => props.alt ? 'red' : 'green'};
9-
color: white;
10-
font: inherit;
11-
border: 1px solid blue;
12-
padding: 8px;
13-
cursor: pointer;
14-
15-
&:hover {
16-
background-color: ${props => props.alt ? 'salmon' : 'lightgreen'};
17-
color: black;
18-
}
19-
`;
4+
import Persons from '../components/Persons/Persons';
5+
import Cockpit from '../components/Cockpit/Cockpit';
6+
207

218
class App extends Component {
229
state = {
@@ -61,35 +48,16 @@ class App extends Component {
6148
};
6249

6350
render() {
64-
const style = {
65-
backgroundColor: 'green',
66-
color: 'white',
67-
font: 'inherit',
68-
border: '1px solid blue',
69-
padding: '8px',
70-
cursor: 'pointer',
71-
':hover': {
72-
backgroundColor: 'lightgreen',
73-
color: 'black'
74-
}
75-
};
7651

7752
let persons = null;
7853

7954
if (this.state.showPersons) {
8055
persons = (
8156
<div>
82-
{this.state.persons.map((person, index) => {
83-
return (
84-
<Person
85-
click={() => this.deletePersonHandler(index)}
86-
name={person.name}
87-
age={person.age}
88-
key={person.id}
89-
changed={event => this.nameChangedHandler(event, person.id)}
90-
/>
91-
);
92-
})}
57+
<Persons
58+
persons={this.state.persons}
59+
clicked={this.deletePersonHandler}
60+
changed={this.nameChangedHandler} />
9361
</div>
9462
);
9563

@@ -100,21 +68,12 @@ class App extends Component {
10068
// };
10169
}
10270

103-
const classes = [];
104-
if (this.state.persons.length <= 2) {
105-
classes.push('red'); // classes = ['red']
106-
}
107-
if (this.state.persons.length <= 1) {
108-
classes.push('bold'); // classes = ['red', 'bold']
109-
}
110-
11171
return (
11272
<div className="App">
113-
<h1>Hi, I'm a React App</h1>
114-
<p className={classes.join(' ')}>This is really working!</p>
115-
<StyledButton alt={this.state.showPersons} onClick={this.togglePersonsHandler}>
116-
Toggle Persons
117-
</StyledButton>
73+
<Cockpit
74+
showPersons={this.state.showPersons}
75+
persons={this.state.persons}
76+
clicked={this.togglePersonsHandler} />
11877
{persons}
11978
</div>
12079
);

0 commit comments

Comments
 (0)