Skip to content

Commit acdb4e7

Browse files
committed
React Hooks Example
1 parent 73d1442 commit acdb4e7

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

my-app/src/App.js

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
1-
import React, {Component} from 'react';
1+
import React, { useState } from 'react';
22
import './App.css';
33
import Person from './Person/Person'
44

5-
class App extends Component {
6-
state = {
5+
const App = props => {
6+
const [ personsState, setPersonsState ] = useState({
77
persons: [
88
{ name: 'Max', age: 28 },
99
{ name: 'Manu', age: 29 },
1010
{ name: 'Stephanie', age: 26 }
1111
],
1212
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+
}
2628

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+
);
3939
}
4040

4141
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

Comments
 (0)