YaleDevUni / comics-web-reader

web based file reader( comics viewer )
https://viewer.yesyeil.ca
MIT License
1 stars 1 forks source link

Rerendering cover images in main page when other states change #15

Closed YaleDevUni closed 10 months ago

YaleDevUni commented 10 months ago

Proposed solution: Instead of using Blob in image src directly, use useEffect for Blob dependency. Example:

  useEffect(() => {
    const fetchBook = async () => {
      const bookId = parseInt(searchParams.get("index") as string, 10);
      const fetchedBook = await db.books.get(bookId);
      setBook(fetchedBook as Book);
      // Create Blob URL once when the book is fetched
      const blobURL = URL.createObjectURL(
        new Blob([fetchedBook?.image as BlobPart], {
          type: "image/webp",
        })
      );
      setImageSrc(blobURL);
    };
    fetchBook();
  }, []);
YaleDevUni commented 10 months ago

Solved using useMemo