-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: useQueryParams 커스텀 훅 작성 * feat: 검색 키워드 유지 기능 추가 * fix: Skeleton 스타일 수정 * fix: global.css에 warning이 나타나던 문제 해결 * feat: 검색 결과 유지 기능 추가 * chore: 변수명 수정 * chore: 주석 추가 * feat: 도서 검색 Input에 검색어 초기화 기능 추가 * fix: unable to verify the first certificate 인증서를 인식못하던 문제 해결 * refactor: useQueryParams 개선 - 불필요한 state 제거 - useCallback 훅 적용 - 잘못 포함된 '/' 제거 - removeQueryParam의 earlyReturn 문 개선 * refactor: queryParams 개선 (state 사용x) * fix: 검색창 search 아이콘 LayoutShift 현상 제거 * fix: setQueryParams 개선
- Loading branch information
Showing
8 changed files
with
118 additions
and
14 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,70 @@ | ||
import { useRouter, useSearchParams } from 'next/navigation'; | ||
import { useCallback } from 'react'; | ||
|
||
type RouteOptions = 'push' | 'replace'; | ||
|
||
type QueryParams = { [key: string]: string }; | ||
|
||
const useQueryParams = () => { | ||
const router = useRouter(); | ||
const searchParams = useSearchParams(); | ||
|
||
const queryParams = searchParams.toString(); | ||
|
||
const getQueryParam = useCallback( | ||
(queryKey: string) => { | ||
const queryParam = searchParams.get(queryKey); | ||
|
||
return queryParam; | ||
}, | ||
[searchParams] | ||
); | ||
|
||
const setQueryParams = useCallback( | ||
(queryParams: QueryParams, option?: RouteOptions) => { | ||
const prevParams = new URLSearchParams(searchParams.toString()); | ||
|
||
for (const queryKey in queryParams) { | ||
prevParams.set(queryKey, queryParams[queryKey]); | ||
} | ||
|
||
const newQueryParams = prevParams.toString(); | ||
|
||
switch (option) { | ||
case 'replace': | ||
router.replace(`?${newQueryParams}`, { shallow: true }); | ||
return; | ||
case 'push': | ||
default: | ||
router.push(`?${newQueryParams}`, { shallow: true }); | ||
return; | ||
} | ||
}, | ||
[router, searchParams] | ||
); | ||
|
||
const removeQueryParam = useCallback( | ||
(queryKey: string, option?: RouteOptions) => { | ||
const prevParams = new URLSearchParams(searchParams.toString()); | ||
if (!prevParams.has(queryKey)) return; | ||
prevParams.delete(queryKey); | ||
|
||
const newQueryParams = prevParams.toString(); | ||
|
||
switch (option) { | ||
case 'replace': | ||
router.replace(`?${newQueryParams}`, { shallow: true }); | ||
return; | ||
case 'push': | ||
default: | ||
router.push(`?${newQueryParams}`, { shallow: true }); | ||
return; | ||
} | ||
}, | ||
[router, searchParams] | ||
); | ||
|
||
return { queryParams, getQueryParam, setQueryParams, removeQueryParam }; | ||
}; | ||
|
||
export default useQueryParams; |
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
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