1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Reactアプリ100本ノックやってみた 「03 TODO」

1
Last updated at Posted at 2023-11-29

はじめに

フロントエンドに苦手意識のある私が、@Sicut_studyさんの【Reactアプリ100本ノック】をやってみました。
変なところがあれば教えていただきたいです。

今回の課題

やってみる

App.tsx
import { useState } from "react";

const App = () => {
  const [todolist, setTodoList] = useState<string[]>([]); // todoリスト
  const [input, setInput] = useState(""); // 入力欄

  const addtodo = () => {
    if (input !== "") {
      setTodoList([...todolist, input]);
      setInput("");
    } else {
      alert("入力してください。");
    }
  };

  // クリックしたボタンに紐づくtodoを削除
  const deleteList = (index: number) => {
    const deleteList = [...todolist];
    deleteList.splice(index, 1);
    setTodoList(deleteList);
  };

  return (
    <div>
      <input
        type="text"
        value={input}
        onChange={(e) => setInput(e.target.value)}
      />
      {/* todoリスト配列に要素を追加する */}
      <button onClick={addtodo}>追加</button>
      {/* todoリスト*/}
      <div>
        {todolist.map((todo, index) => {
          return (
            <div>
              <span>{todo}</span>
              <button
                onClick={() => {
                  deleteList(index);
                }}
              >
                削除
              </button>
            </div>
          );
        })}
      </div>
    </div>
  );
};

export default App;

完成

https___qiita-image-store.s3.ap-northeast-1.amazonaws.com_0_3621123_95afc035-c449-59e1-efc9-8414d127256f.gif

おわりに

見た目が簡素すぎるのですが、要件満たしたのでいったん完了とします。
引き続き頑張ります。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?