• + 0 comments

    Easy solution just using 3 state variables, but surely can be optomized a lot.

    Below is the code:

    import React, { useState } from "react"; import Input from "./Input"; import PostDisplay from "./PostDisplay";

    function Home() { const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [posts, setPosts] = useState([]);

    const handlePost = () => { if (title !== "" && description !== "") { const id = posts.length; setPosts([...posts, { id: id, title: title, description: description }]); setTitle(""); setDescription(""); } }

    const handleDeletePost = (id) => { const updatedPosts = posts.filter((item) => item.id !== id); setPosts([...updatedPosts]); }

    return ( Create Post ); }

    export default Home;