|
1 | 1 | import React, { Component } from 'react'; |
2 | | -import logo from './logo.svg'; |
3 | 2 | import './App.css'; |
| 3 | +import Person from './Person/Person'; |
| 4 | + |
4 | 5 |
|
5 | 6 | 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 | + |
6 | 34 | render() { |
| 35 | + //inline styles |
| 36 | + const style = { |
| 37 | + backgroundColor: 'white', |
| 38 | + font: 'inherit', |
| 39 | + border: '1px solid blue', |
| 40 | + padding: '8px' |
| 41 | + }; |
| 42 | + |
7 | 43 | 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?')); |
8 | 46 | <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 | + /> |
23 | 66 | </div> |
24 | 67 | ); |
| 68 | + |
25 | 69 | } |
26 | 70 | } |
27 | 71 |
|
|
0 commit comments