|
1 | | -import React, { useState } from "react"; |
| 1 | +import React, { useState, useCallback } from "react"; |
2 | 2 | import { useGlobalContext } from "../Context"; |
3 | 3 |
|
4 | 4 | const Bookmarks = () => { |
5 | 5 | const { bookMark, setBookMark } = useGlobalContext(); |
6 | 6 | const [removingId, setRemovingId] = useState(null); |
7 | 7 |
|
8 | | - if (!bookMark.length) { |
9 | | - return ( |
10 | | - <div style={{ display: "grid", placeItems: "center", height: "100vh" }}> |
11 | | - <h1>No Bookmarks Yet! ⭐</h1> |
12 | | - </div> |
13 | | - ); |
14 | | - } |
15 | | - |
16 | | - const removeBookmark = (news) => { |
17 | | - setRemovingId(news.objectID); |
| 8 | + const removeBookmark = useCallback((news) => { |
| 9 | + setRemovingId(news.objectID); |
18 | 10 |
|
19 | 11 | setTimeout(() => { |
20 | 12 | const updatedBookmarks = bookMark.filter( |
21 | 13 | (item) => item.objectID !== news.objectID |
22 | 14 | ); |
23 | 15 | setBookMark(updatedBookmarks); |
24 | 16 | localStorage.setItem("bookmarks", JSON.stringify(updatedBookmarks)); |
25 | | - }, 300); |
26 | | - }; |
| 17 | + setRemovingId(null); |
| 18 | + }, 300); |
| 19 | + }, [bookMark, setBookMark]); |
| 20 | + |
| 21 | + if (!bookMark.length) { |
| 22 | + return ( |
| 23 | + <div style={{ display: "grid", placeItems: "center", height: "100vh" }}> |
| 24 | + <h1>No Bookmarks Yet! ⭐</h1> |
| 25 | + </div> |
| 26 | + ); |
| 27 | + } |
27 | 28 |
|
28 | 29 | return ( |
29 | 30 | <> |
30 | 31 | <div className="stories-div" style={{ paddingTop: "3rem" }}> |
31 | 32 | {bookMark.map((curPost) => { |
32 | 33 | const { title, author, objectID, url, num_comments } = curPost; |
| 34 | + const isRemoving = removingId === objectID; |
| 35 | + |
33 | 36 | return ( |
34 | 37 | <div |
35 | | - className={`card ${removingId === objectID ? "removing" : ""}`} |
| 38 | + className={`card ${isRemoving ? "removing" : ""}`} |
36 | 39 | key={objectID} |
37 | 40 | > |
38 | 41 | <h2>{title}</h2> |
|
0 commit comments