Created
April 1, 2020 03:02
-
-
Save francodalessio/0eab1d3393cf89603854649a2d17a463 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 from "react"; | |
import useInput from "./hooks/useInput"; | |
import "./TodoApp.css"; | |
import useArray from "./hooks/useArray"; | |
export default function TodoApp() { | |
const [todo, setTodo, resetTodo] = useInput(""); | |
const todos = useArray([]); | |
const onSubmit = e => { | |
e.preventDefault(); | |
if (todo === "") return; | |
todos.addItem(todo); | |
resetTodo(); | |
}; | |
return ( | |
<div className="container"> | |
<form onSubmit={onSubmit}> | |
<label htmlFor="todo">Todo text</label> | |
<br /> | |
<input | |
id="todo" | |
className="todo-input" | |
onChange={setTodo} | |
value={todo} | |
/> | |
<button type="submit" className="add-btn"> | |
Add | |
</button> | |
</form> | |
<div> | |
<ul> | |
{todos.list.map(todo => ( | |
<li key={todo.id}> | |
<span | |
className={todo.completed ? "todo-completed" : undefined} | |
onClick={() => todos.toggleItem(todo.id)} | |
> | |
{todo.text} | |
</span> | |
<span | |
className="delete-btn" | |
onClick={() => todos.removeItem(todo.id)} | |
> | |
x | |
</span> | |
</li> | |
))} | |
</ul> | |
</div> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment