|
1 |
| -import React, {Component} from 'react'; |
| 1 | +import React, { useState } from 'react'; |
2 | 2 | import './App.css';
|
3 | 3 | import Person from './Person/Person'
|
4 | 4 |
|
5 |
| -class App extends Component { |
6 |
| - state = { |
| 5 | +const App = props => { |
| 6 | + const [ personsState, setPersonsState ] = useState({ |
7 | 7 | persons: [
|
8 | 8 | { name: 'Max', age: 28 },
|
9 | 9 | { name: 'Manu', age: 29 },
|
10 | 10 | { name: 'Stephanie', age: 26 }
|
11 | 11 | ],
|
12 | 12 | otherState: 'some other value'
|
13 |
| - } |
14 |
| - |
15 |
| - switchNameHandler = () => { |
16 |
| - // console.log('Was clicked') |
17 |
| - // DON'T DO THIS: this.state.persons[0].name='Maximilian'; |
18 |
| - this.setState( { |
19 |
| - persons: [ |
20 |
| - { name: 'Maximilian', age: 28 }, |
21 |
| - { name: 'Manu', age: 29 }, |
22 |
| - { name: 'Stephanie', age: 27 } |
23 |
| - ] |
24 |
| - }) |
25 |
| - } |
| 13 | + }); |
| 14 | + |
| 15 | +const [ otherState, setOtherState ] = useState('some other value'); |
| 16 | +console.log(personsState, otherState) |
| 17 | +const switchNameHandler = () => { |
| 18 | + // console.log('Was clicked') |
| 19 | + // DON'T DO THIS: this.state.persons[0].name='Maximilian'; |
| 20 | + setPersonsState( { |
| 21 | + persons: [ |
| 22 | + { name: 'Maximilian', age: 28 }, |
| 23 | + { name: 'Manu', age: 29 }, |
| 24 | + { name: 'Stephanie', age: 27 } |
| 25 | + ] |
| 26 | + }) |
| 27 | +} |
26 | 28 |
|
27 |
| - render(){ |
28 |
| - return ( |
29 |
| - <div className="App"> |
30 |
| - <h1>Hi, I'm a React App</h1> |
31 |
| - <p>This is really working!</p> |
32 |
| - <button onClick={this.switchNameHandler}>Switch Name</button> |
33 |
| - <Person name={this.state.persons[0].name} age={this.state.persons[0].age}/> |
34 |
| - <Person name={this.state.persons[1].name} age={this.state.persons[1].age}>My Hobbies: Racing</Person> |
35 |
| - <Person name={this.state.persons[2].name} age={this.state.persons[2].age}/> |
36 |
| - </div> |
37 |
| - ); |
38 |
| - } |
| 29 | + return ( |
| 30 | + <div className="App"> |
| 31 | + <h1>Hi, I'm a React App</h1> |
| 32 | + <p>This is really working!</p> |
| 33 | + <button onClick={switchNameHandler}>Switch Name</button> |
| 34 | + <Person name={personsState.persons[0].name} age={personsState.persons[0].age}/> |
| 35 | + <Person name={personsState.persons[1].name} age={personsState.persons[1].age}>My Hobbies: Racing</Person> |
| 36 | + <Person name={personsState.persons[2].name} age={personsState.persons[2].age}/> |
| 37 | + </div> |
| 38 | + ); |
39 | 39 | }
|
40 | 40 |
|
41 | 41 | export default App;
|
| 42 | + |
| 43 | +// state = { |
| 44 | +// persons: [ |
| 45 | +// { name: 'Max', age: 28 }, |
| 46 | +// { name: 'Manu', age: 29 }, |
| 47 | +// { name: 'Stephanie', age: 26 } |
| 48 | +// ], |
| 49 | +// otherState: 'some other value' |
| 50 | +// } |
0 commit comments