0
0

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.

ReactJS 簡単なブログアプリケーションを作ってみた 📔

Last updated at Posted at 2022-07-23

完成したアプリケーション:tada:

2022-07-23_16h57_05.gif

こちらのリンクからも確認できます:arrow_down:

開発環境

今回は、CodeSandboxを使って開発を進めていきます:rocket:

image.png

Create SandboxからReactを選択してください!

image.png

Reactプロジェクトがセットアップされます!

image.png

それぞれ該当するファイルに下記のソースコードをコピーペーストしてください!

App.js
import { useState } from "react";
import "./styles.css";

export default function App() {
  const [blogs, setBlogs] = useState([
    { id: 1, author: "mario", title: "First", body: "First  post" },
    { id: 2, author: "mario", title: "Second", body: "Second post" }
  ]);

  const [title, setTitle] = useState("");
  const [author, setAuthor] = useState("");
  const [body, setBody] = useState("");
  const [id, setId] = useState(0);

  const [isEdit, setIsEdit] = useState(false);

  // Add
  const handleAddPost = () => {
    if (!title || !author || !body) {
      return alert("Please input something ");
    }

    const newPost = {
      id: Math.floor(Math.random() * 1000),
      author: author,
      title: title,
      body: body
    };

    setBlogs([...blogs, newPost]);

    setTitle("");
    setAuthor("");
    setBody("");
  };

  // Delete
  const deletePost = (id) => {
    const newBlogs = blogs.filter((blog) => blog.id !== id);
    setBlogs(newBlogs);
  };

  // Edit
  const handleEdit = () => {
    setBlogs(
      blogs.map((blog) => {
        return blog.id === id
          ? { ...blog, title: title, author: author, body: body }
          : blog;
      })
    );

    setTitle("");
    setAuthor("");
    setBody("");
    setId(0);
    setIsEdit(false);
  };

  const editPost = (blog) => {
    setIsEdit(true);

    setTitle(blog.title);
    setAuthor(blog.author);
    setBody(blog.body);
    setId(blog.id);

    window.scroll({ top: 0, behavior: "smooth" });
  };

  return (
    <div className="App">
      <h1>All Blogs</h1>
      <h2>Total posts :{blogs.length}</h2>

      <div className="inputForm">
        <input
          type="text"
          placeholder="Title..."
          value={title}
          onChange={(e) => setTitle(e.target.value)}
        />
        <input
          type="text"
          placeholder="Author..."
          value={author}
          onChange={(e) => setAuthor(e.target.value)}
        />
        <input
          type="text"
          placeholder="Body..."
          value={body}
          onChange={(e) => setBody(e.target.value)}
        />

        {isEdit ? (
          <button className="add-btn" onClick={handleEdit}>
            Edit
          </button>
        ) : (
          <button className="add-btn" onClick={handleAddPost}>
            Add Post
          </button>
        )}
      </div>

      {blogs.map((blog) => (
        <div key={blog.id} className="card">
          <div>
            <h1>Title : {blog.title}</h1>
            <p>Body : {blog.body}</p>
            <p>Author : {blog.author}</p>
          </div>

          <div>
            <button onClick={() => deletePost(blog.id)} className="delete-btn">
              Delete
            </button>
            <button onClick={() => editPost(blog)} className="edit-btn">
              Edit
            </button>
          </div>
        </div>
      ))}
    </div>
  );
}
styles.css
.App {
  font-family: sans-serif;
  text-align: center;
  align-items: center;
}

.inputForm {
  display: flex;
  flex-direction: column;
  width: 30%;
  margin: auto;
}

input {
  padding: 5px;
  margin-top: 5px;
}

.add-btn {
  padding: 5px;
  width: 50%;
  margin: 5px auto;
  background-color: #329eff;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid black;
}

.add-btn:hover {
  background-color: #b8dcf5;
}

.delete-btn {
  padding: 5px;
  width: 30%;
  margin: auto;
  background-color: #f16f6f;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid rgb(214, 18, 18);
}

.delete-btn:hover {
  background-color: #f5bbc0;
}

.edit-btn {
  padding: 5px;
  width: 30%;
  margin: auto;
  background-color: #45eb90;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid green;
}

.edit-btn:hover {
  background-color: #ade7c6;
}

.card {
  border: solid 2px #329eff;
  width: 60%;
  border-radius: 20px;
  padding: 4px;
  margin: 8px auto;
  background-color: rgb(243, 243, 230);
}

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?