forked from geeksblabla/30DaysOfReact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge.js
42 lines (36 loc) · 898 Bytes
/
challenge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class MyComponent extends React.Component {
constructor() {
super();
this.state = {
names: ['name 1', 'name 2', 'name 3'],
}
}
addName = () => {
// this function is correct
const name = document.querySelector("#input").value;
if(name.length === 0){
alert("insert a valid name");
return;
}
let names = this.state.names;
names.push(name);
this.setState({names})
}
render () {
const {names} = this.state;
const items = [];
return (
<label>Name: </label>
<input type="text" id="input" />
<button onClick={this.addName}>Add</button>
<br>
<ul>{
for (const [index, value] of names.entries()) {
items.push(<li key={index}>{value}</li>)
}
}
</ul>
);
}
}
ReactDOM.render(<MyComponent />, document.getElementById('root'));