-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc9b687
commit eff9150
Showing
4 changed files
with
71 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useCallback, useEffect, useRef } 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); | ||
return prev; | ||
}, | ||
{ replace: true } | ||
); | ||
}, | ||
[setSearchParams] | ||
); | ||
|
||
useEffect(() => { | ||
if (count.current === 0) { | ||
for (const key in initialParams) { | ||
if (initialParams[key]) { | ||
const element = initialParams[key]; | ||
updateParamsValue(key, element); | ||
} | ||
} | ||
count.current = 1; | ||
} | ||
}, [updateParamsValue, initialParams]); | ||
|
||
return { searchParams, setSearchParams, updateParamsValue }; | ||
}; | ||
|
||
export default useParamsValue; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters