Skip to content

Instantly share code, notes, and snippets.

@alecklandgraf
Created December 24, 2018 22:16
Show Gist options
  • Save alecklandgraf/60aba5b53de3228b7e7aeea6e07938af to your computer and use it in GitHub Desktop.
Save alecklandgraf/60aba5b53de3228b7e7aeea6e07938af to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
function App() {
const [todos, updateTodos] = useState([]);
const [currentTodo, updateTodo] = useState("");
return (
<div className="App">
<h3>Todos</h3>
{todos.map(({ todo, ts }) => (
<li key={ts}>
{todo}
<button
onClick={() =>
updateTodos(todos.filter(({ ts: todoTs }) => todoTs !== ts))
}
>
X
</button>
</li>
))}
<input
type="text"
value={currentTodo}
placeholder="Enter a todo..."
onChange={e => updateTodo(e.target.value)}
onKeyPress={e => {
if (e.key === "Enter" && currentTodo) {
updateTodos([...todos, { todo: currentTodo, ts: Date.now() }]);
updateTodo("");
}
}}
/>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment