Skip to content

Instantly share code, notes, and snippets.

@youliangdao
Last active December 3, 2022 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youliangdao/128299f74c063053aba0c0a2efa4de76 to your computer and use it in GitHub Desktop.
Save youliangdao/128299f74c063053aba0c0a2efa4de76 to your computer and use it in GitHub Desktop.
import "./App.css";
import { useState } from "react";
function App() {
const [todos, setTodos] = useState([]);
const [idCounter, setIdCounter] = useState(0);
const handleSubmit = (e) => {
e.preventDefault();
const inputText = e.target["task"].value;
const nextid = idCounter + 1;
setIdCounter(nextid);
setTodos([...todos, { id: nextid, task: inputText, checked: false }]);
};
const handleChangeCheckBox = (id) => {
const changedTodos = todos.map((todo) => {
if (todo.id === id) {
return { ...todo, checked: !todo.checked };
}
return todo;
});
setTodos(changedTodos);
};
const handleClickDeleteButton = (id) => {
setTodos(todos.filter((todo) => todo.id !== id));
};
return (
<div className="App">
<h1>TODOリスト</h1>
<form onSubmit={handleSubmit}>
<input name="task" />
<button>登録</button>
</form>
<div>
{todos.map((todo) => (
<div key={todo.id} className={todo.checked ? "checked" : ""}>
<input
type="checkbox"
onChange={() => handleChangeCheckBox(todo.id)}
/>
{todo.task}
<button onClick={() => handleClickDeleteButton(todo.id)}>
削除
</button>
</div>
))}
</div>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment