Created
September 12, 2019 09:26
-
-
Save juhahinkula/12ef585ff2298be30a5f51c44d602b0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import './App.css'; | |
function App() { | |
const [todo, setTodo] = useState({desc: '', date: ''}); | |
const [todos, setTodos] = useState([]); | |
const addTodo = (event) => { | |
event.preventDefault(); | |
setTodos([...todos, todo]); | |
} | |
const deleteItem = (event) => { | |
event.preventDefault(); | |
setTodos(todos.filter((item, index) => parseInt(event.target.id) !== index)) | |
} | |
const inputChanged = (event) => { | |
setTodo({...todo, [event.target.name]: event.target.value}); | |
} | |
return ( | |
<div className="App"> | |
<form onSubmit={addTodo}> | |
<input type="date" name="date" value={todo.date} onChange={inputChanged}/> | |
<input type="text" name="desc" value={todo.desc} onChange={inputChanged}/> | |
<input type="submit" value="Add" /> | |
<table><tbody> | |
{ | |
todos.map((todo, index) => | |
<tr key={index}> | |
<td>{todo.date}</td> | |
<td>{todo.desc}</td> | |
<td><button id={index} onClick={deleteItem}>Delete</button></td> | |
</tr>) | |
} | |
</tbody></table> | |
</form> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment