Advertisement
Guest User

Untitled

a guest
Apr 6th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //App Component
  2.  
  3.     <div>
  4.       <h1>Lama Book Shop</h1>
  5.       <div className="books">
  6.         {books.map((book) => (
  7.           <div key={book.id} className="book">
  8.             <img src={book.cover} alt="" />
  9.             <h2>{book.title}</h2>
  10.             <p>{book.desc}</p>
  11.             <span>${book.price}</span>
  12.             <button className="delete" onClick={() => handleDelete(book.id)}>
  13.               Delete
  14.             </button>
  15.             <button className="update">
  16.               <Link
  17.                 to={{
  18.                   pathname: `/update/${book.id}`,
  19.                   state: {'bookId': book.id},
  20.                 }}
  21.                 style={{ color: "inherit", textDecoration: "none" }}
  22.               >
  23.                 Update
  24.               </Link>
  25.             </button>
  26.           </div>
  27.         ))}
  28.       </div>
  29.  
  30.  
  31. //Router Component
  32.  
  33. function App() {
  34.   return (
  35.     <div className="app">
  36.       <BrowserRouter>
  37.         <Routes>
  38.           <Route path="/" element={<Books />} />
  39.           <Route path="/add" element={<Add />} />
  40.           <Route path="/update/:id" element={<Update />} />
  41.         </Routes>
  42.       </BrowserRouter>
  43.     </div>
  44.   );
  45. }
  46.  
  47. //Update (Null Stste component)
  48. const Update = () => {
  49.   const location = useLocation();
  50.   const navigate = useNavigate();
  51.   const bookFromState = location.state?.bookId; #NULL
  52.   console.log("test")
  53.   console.log(location.state) #NULL
  54.  
  55.   const [book, setBook] = useState(bookFromState || {
  56.     title: "",
  57.     desc: "",
  58.     price: null,
  59.     cover: "",
  60.   });
  61.   const [error,setError] = useState(false)
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement