Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeparticle committed Sep 24, 2023
1 parent eff9150 commit edbc9f6
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions ui/src/lib/utils/hooks/useParamsValue.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { useCallback, useEffect, useRef } from "react";
import { useCallback, useEffect } from "react";
import { useSearchParams } from "react-router-dom";

interface Params {
[key: string]: string;
}

const useParamsValue = (initialParams: Params) => {
const count = useRef(0);

const [searchParams, setSearchParams] = useSearchParams(initialParams);

const updateParamsValue = useCallback(
(key: string, value: string) => {
console.log(key, value);
setSearchParams(
(prev) => {
prev.set(key, value);
Expand All @@ -25,16 +22,14 @@ const useParamsValue = (initialParams: Params) => {
);

useEffect(() => {
if (count.current === 0) {
for (const key in initialParams) {
if (initialParams[key]) {
const element = initialParams[key];
updateParamsValue(key, element);
}
for (const key in initialParams) {
if (initialParams[key]) {
const element = initialParams[key];
updateParamsValue(key, element);
}
count.current = 1;
}
}, [updateParamsValue, initialParams]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return { searchParams, setSearchParams, updateParamsValue };
};
Expand Down

0 comments on commit edbc9f6

Please sign in to comment.