Skip to content

Commit 0a4b562

Browse files
committed
Update onClick function
1 parent acdb4e7 commit 0a4b562

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

my-app/src/App.js

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

5-
const App = props => {
6-
const [ personsState, setPersonsState ] = useState({
5+
class App extends Component {
6+
state = {
77
persons: [
88
{ name: 'Max', age: 28 },
99
{ name: 'Manu', age: 29 },
1010
{ name: 'Stephanie', age: 26 }
1111
],
1212
otherState: 'some other value'
13-
});
13+
};
1414

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-
}
15+
switchNameHandler = (newName) => {
16+
// console.log('Was clicked')
17+
// DON'T DO THIS: this.state.persons[0].name='Maximilian';
18+
this.setState( {
19+
persons: [
20+
{ name: newName, age: 28 },
21+
{ name: 'Manu', age: 29 },
22+
{ name: 'Stephanie', age: 27 }
23+
]
24+
})
25+
}
2826

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-
);
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('Maximilan!!')}>Switch Name</button>
33+
<Person name={this.state.persons[0].name} age={this.state.persons[0].age}/>
34+
<Person
35+
name={this.state.persons[1].name}
36+
age={this.state.persons[1].age}
37+
click={this.switchNameHandler.bind(this, "Max!")}>My Hobbies: Racing</Person>
38+
<Person name={this.state.persons[2].name} age={this.state.persons[2].age}/>
39+
</div>
40+
)
41+
}
3942
}
40-
4143
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-
// }

my-app/src/Person/Person.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
const person = (props) => {
44
return (
55
<div>
6-
<p>I'm {props.name} and I am {props.age} years old!</p>
6+
<p onClick={props.click}>I'm {props.name} and I am {props.age} years old!</p>
77
<p>{props.children}</p>
88
</div>
99
);

0 commit comments

Comments
 (0)