Skip to content

Commit

Permalink
fix useIsScrollable dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Oct 12, 2023
1 parent 60d8223 commit ed97541
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/Common/hooks/useIsScrollable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useRef, useEffect } from "react";

const useIsScrollable = () => {
const useIsScrollable = (deps?: unknown[]) => {
const [isScrollable, setIsScrollable] = useState(false);
const ref = useRef<HTMLDivElement>(null);

Expand All @@ -27,7 +27,7 @@ const useIsScrollable = () => {
return () => {
div?.removeEventListener("scroll", handleScroll);
};
}, [ref.current]); // Re-run effect if divRef.current changes
}, [ref.current, ...(deps || [])]); // Re-run effect if divRef.current changes

return { isScrollable, ref };
};
Expand Down
15 changes: 2 additions & 13 deletions src/Components/Medicine/MedicineAdministrationSheet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useTranslation } from "react-i18next";
import useSlug from "../../../Common/hooks/useSlug";
import useQuery from "../../../Utils/request/useQuery";
import MedicineRoutes from "../routes";
import { useEffect, useMemo, useRef, useState } from "react";
import { useMemo, useState } from "react";
import { computeActivityBounds } from "./utils";
import useBreakpoints from "../../../Common/hooks/useBreakpoints";
import SubHeading from "../../../CAREUI/display/SubHeading";
Expand All @@ -25,8 +25,6 @@ const MedicineAdministrationSheet = ({ readonly, is_prn }: Props) => {
const { t } = useTranslation();
const consultation = useSlug("consultation");

const { isScrollable, ref } = useIsScrollable();

const [showDiscontinued, setShowDiscontinued] = useState(false);

const filters = { is_prn, prescription_type: "REGULAR", limit: 100 };
Expand Down Expand Up @@ -62,16 +60,7 @@ const MedicineAdministrationSheet = ({ readonly, is_prn }: Props) => {
defaultEnd: true,
});

const divRef = useRef<HTMLDivElement>(null);

useEffect(() => {
if (divRef.current) {
const isScrollable =
divRef.current.scrollHeight > divRef.current.clientHeight;

console.log(`Is div scrollable? ${isScrollable ? "Yes" : "No"}`);
}
}, [prescriptions]);
const { isScrollable, ref } = useIsScrollable([data?.results?.length]);

return (
<div>
Expand Down

0 comments on commit ed97541

Please sign in to comment.