Skip to content

Commit 8809a8a

Browse files
author
Barbara Ormonde
committed
A simple exercice on ReactJS
1 parent 58c1fb0 commit 8809a8a

File tree

6 files changed

+83
-61
lines changed

6 files changed

+83
-61
lines changed

src/App.css

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,3 @@
11
.App {
22
text-align: center;
3-
}
4-
5-
.App-logo {
6-
animation: App-logo-spin infinite 20s linear;
7-
height: 40vmin;
8-
}
9-
10-
.App-header {
11-
background-color: #282c34;
12-
min-height: 100vh;
13-
display: flex;
14-
flex-direction: column;
15-
align-items: center;
16-
justify-content: center;
17-
font-size: calc(10px + 2vmin);
18-
color: white;
19-
}
20-
21-
.App-link {
22-
color: #61dafb;
23-
}
24-
25-
@keyframes App-logo-spin {
26-
from {
27-
transform: rotate(0deg);
28-
}
29-
to {
30-
transform: rotate(360deg);
31-
}
32-
}
3+
}

src/App.js

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,71 @@
11
import React, { Component } from 'react';
2-
import logo from './logo.svg';
32
import './App.css';
3+
import Person from './Person/Person';
4+
45

56
class App extends Component {
7+
//state is managed inside a component and only called on classes that extends Component. It's not a function component
8+
state = {
9+
persons: [
10+
{name: 'BMO', age: 28},
11+
{name: 'Max', age: 28},
12+
{name: 'Viking', age: 9}
13+
]
14+
}
15+
16+
switchNameHandler = (newName) => {
17+
//DON'T DO THIS: this.state.persons[0].name = 'Barbara';
18+
//this will merge with existing data
19+
this.setState({persons: [
20+
{name: 'Barbara', age: 28},
21+
{name: newName, age: 28},
22+
{name: 'Viking, the Golden Rascal', age: 9}
23+
]})
24+
}
25+
26+
nameChangedHandler = (event) => {
27+
this.setState({persons: [
28+
{name: event.target.value, age: 28},
29+
{name: 'Max', age: 28},
30+
{name: 'Viking, the Golden Rascal', age: 9}
31+
]})
32+
}
33+
634
render() {
35+
//inline styles
36+
const style = {
37+
backgroundColor: 'white',
38+
font: 'inherit',
39+
border: '1px solid blue',
40+
padding: '8px'
41+
};
42+
743
return (
44+
//este é igual ao return HTML exclusivo de baixo, mas formatado diferente
45+
//return React.createElement('div', {className: 'App'}, React.createElement('h1', null, 'Does this work now?'));
846
<div className="App">
9-
<header className="App-header">
10-
<img src={logo} className="App-logo" alt="logo" />
11-
<p>
12-
Edit <code>src/App.js</code> and save to reload.
13-
</p>
14-
<a
15-
className="App-link"
16-
href="https://reactjs.org"
17-
target="_blank"
18-
rel="noopener noreferrer"
19-
>
20-
Learn React
21-
</a>
22-
</header>
47+
<h1>Hi, I'm a React App</h1>
48+
<p> This is really working</p>
49+
<button
50+
style={style}
51+
onClick={() => this.switchNameHandler('Maximilian!!')}>Switch Name</button>
52+
<Person
53+
name={this.state.persons[0].name}
54+
age={this.state.persons[0].age}
55+
changed={this.nameChangedHandler}/>
56+
<Person
57+
name={this.state.persons[1].name}
58+
age={this.state.persons[1].age}
59+
changed={this.nameChangedHandler}
60+
click={this.switchNameHandler.bind(this, 'Max!')}>My Hobbies: Racing</Person>
61+
<Person
62+
name={this.state.persons[2].name}
63+
age={this.state.persons[2].age}
64+
changed={this.nameChangedHandler}
65+
/>
2366
</div>
2467
);
68+
2569
}
2670
}
2771

src/Person/Person.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.Person{
2+
width: 60%;
3+
margin: 16px auto;
4+
border: 1px solid #eee;
5+
box-shadow: 0 2px 3px #ccc;
6+
padding: 16px;
7+
text-align: center;
8+
}

src/Person/Person.js

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.css';
3+
4+
const person = (props) => {
5+
return (
6+
<div className="Person">
7+
<p onClick={props.click}>I'm {props.name} and I'm {props.age} years old!</p>
8+
<p>{props.children}</p>
9+
<input type="text" onChange={props.changed} />
10+
</div>
11+
)
12+
};
13+
14+
export default person;

src/index.css

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
body {
22
margin: 0;
33
padding: 0;
4-
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
5-
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
6-
sans-serif;
7-
-webkit-font-smoothing: antialiased;
8-
-moz-osx-font-smoothing: grayscale;
4+
font-family: sans-serif;
95
}
106

11-
code {
12-
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
13-
monospace;
14-
}

src/logo.svg

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)