-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFruitsArray.js
30 lines (30 loc) · 897 Bytes
/
FruitsArray.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
import React,{Component} from 'react'
class FruitsArray extends Component{
constructor(props){
super(props);
this.state={
fruits:[],
}
}
saveInput=(e)=>{
this.setState({inputValue:e.target.value});
}
addNewItem=()=>{
this.setState({fruits:[...this.state.fruits,this.state.inputValue]});
console.log(this.state.fruits);
}
render(){
return(
<div>
<input type="text" onChange={this.saveInput}/>
<button type="button" onClick={this.addNewItem}>Add</button>
<ul>
{this.state.fruits.length>0 && this.state.fruits.map((ele)=>{
return <li key={ele.index}>{ele}</li>
})}
</ul>
</div>
)
}
}
export default FruitsArray;