refactor: debounce.jsx to typescript and moving it to Hooks
This commit is contained in:
1 parent
07b0483352
commit
e75606aad3
4 files changed
+31
-20
No files matched your search
@@ -0,0 +1,29 @@
|
|||||||
|
import { useState, useEffect } from "react";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom hook for debouncing values
|
||||||
|
* @template T - The type of the value being debounced
|
||||||
|
* @param value - The value to debounce
|
||||||
|
* @param delay - The debounce delay in milliseconds
|
||||||
|
* @returns The debounced value
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const debouncedSearch = useDebounce(search, 300); // Debounces the search input with a 300ms delay
|
||||||
|
*/
|
||||||
|
const useDebounce = <T,>(value: T, delay: number): T => {
|
||||||
|
const [debouncedValue, setDebouncedValue] = useState<T>(value);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handler = setTimeout(() => {
|
||||||
|
setDebouncedValue(value);
|
||||||
|
}, delay);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
clearTimeout(handler);
|
||||||
|
};
|
||||||
|
}, [value, delay]);
|
||||||
|
|
||||||
|
return debouncedValue;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useDebounce;
|
||||||
@@ -20,7 +20,7 @@ import { useSelector, useDispatch } from "react-redux";
|
|||||||
import { setRowsPerPage } from "@/Features/UI/uiSlice.js";
|
import { setRowsPerPage } from "@/Features/UI/uiSlice.js";
|
||||||
import type { RootState } from "@/Types/state";
|
import type { RootState } from "@/Types/state";
|
||||||
import { InfraMonitorsTable } from "./Components/MonitorsTable";
|
import { InfraMonitorsTable } from "./Components/MonitorsTable";
|
||||||
import useDebounce from "@/Utils/debounce";
|
import useDebounce from "@/Hooks/useDebounce";
|
||||||
|
|
||||||
const InfrastructureMonitors = () => {
|
const InfrastructureMonitors = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import { setRowsPerPage } from "@/Features/UI/uiSlice.js";
|
|||||||
import { useIsAdmin } from "@/Hooks/useIsAdmin";
|
import { useIsAdmin } from "@/Hooks/useIsAdmin";
|
||||||
import type { RootState } from "@/Types/state";
|
import type { RootState } from "@/Types/state";
|
||||||
import { useTheme } from "@mui/material";
|
import { useTheme } from "@mui/material";
|
||||||
import useDebounce from "@/Utils/debounce";
|
import useDebounce from "@/Hooks/useDebounce";
|
||||||
|
|
||||||
const UptimeMonitorsPage = () => {
|
const UptimeMonitorsPage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
import { useState, useEffect } from "react";
|
|
||||||
|
|
||||||
const useDebounce = (value, delay) => {
|
|
||||||
const [debouncedValue, setDebouncedValue] = useState(value);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const handler = setTimeout(() => {
|
|
||||||
setDebouncedValue(value);
|
|
||||||
}, delay);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
clearTimeout(handler);
|
|
||||||
};
|
|
||||||
}, [value, delay]);
|
|
||||||
return debouncedValue;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default useDebounce;
|
|
||||||
Reference in new issue
Block a user