1import React from 'react';
2
3const initialList = [
4 'Learn React',
5 'Learn Firebase',
6 'Learn GraphQL',
7];
8
9const ListWithAddItem = () => {
10 const [value, setValue] = React.useState('');
11 const [list, setList] = React.useState(initialList);
12
13 const handleChange = event => {
14 setValue(event.target.value);
15 };
16
17 const handleSubmit = event => {
18 if (value) {
19 setList(list.concat(value));
20 }
21
22 setValue('');
23
24 event.preventDefault();
25 };
26
27 return (
28 <div>
29 <ul>
30 {list.map(item => (
31 <li key={item}>{item}</li>
32 ))}
33 </ul>
34
35 <form onSubmit={handleSubmit}>
36 <input type="text" value={value} onChange={handleChange} />
37 <button type="submit">Add Item</button>
38 </form>
39 </div>
40 );
41};
42
43export default ListWithAddItem;