<dialog ref={dialogRef} className="h-[12rem] w-[20rem] py-4 px-6 rounded-md…
- User
document id are inside event.target.dataset.id
how do i change the hard coded title and author to match the id's title
- Assistant
To dynamically change the title and author based on an ID, you should first ensure you have a structure (like a list or object) where you can look up titles and authors by their IDs. Let's say you have a list of books where each book has an ID, title, and author. Then, you need a mechanism to pass the selected ID to your component and fetch the title and author from your books list based on this ID.
- User
"use client";
import BookForm from "@/components/bookform"; import { useEffect, useRef, useState } from "react"; import { useFormState } from "react-dom"; import { createBook, getBooks, deleteBook } from "@/actions/books";
export default function Books() { const [books, setBooks] = useState([]); const [formState, formAction] = useFormState(createBook); const dialogRef = useRef(null);
async function editHandler(event) { dialogRef.current.showModal(); }
async function deleteHandler(event) { console.log(event.target.dataset.id); if (confirm("Are you sure you want to delete this book?")) { await deleteBook(event.target.dataset.id); setBooks(await getBooks()); } }
useEffect( function () { getBooks().then((books) => setBooks(books)); }, [formState] );
- Assistant
- User
"use client";
import BookForm from "@/components/bookform"; import { useEffect, useRef, useState } from "react"; import { useFormState } from "react-dom"; import { createBook, getBooks, deleteBook } from "@/actions/books";
export default function Books() { const [books, setBooks] = useState([]); const [currentBook, setCurrentBook] = useState(null); // Added state for the current book const [formState, formAction] = useFormState(createBook); const dialogRef = useRef(null);
- Assistant
Alright, let's try to explain this code in a way that a 10-year-old might understand: