-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
> 对 versions 页面添加 tags、versions query 参数, closes #63 * 添加 `useQueryState` hook,同步 state 和 query 参数 * 支持 {prod, all},默认为 prod 且隐藏对应参数
- Loading branch information
Showing
2 changed files
with
73 additions
and
4 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,64 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { useRouter } from 'next/router'; | ||
|
||
type QueryValue = string | boolean | number; | ||
|
||
function useQueryState<T extends QueryValue>( | ||
key: string, | ||
defaultValue: T, | ||
ignoreValues: QueryValue[] = [] | ||
): [T, (newValue: T) => void] { | ||
const router = useRouter(); | ||
const [state, setState] = useState<T>(() => { | ||
const valueFromQuery = router.isReady ? router.query[key] : undefined; | ||
return parseValue(valueFromQuery, defaultValue); | ||
}); | ||
|
||
useEffect(() => { | ||
if (!router.isReady) return; | ||
const valueFromQuery = router.query[key]; | ||
setState(parseValue(valueFromQuery, defaultValue)); | ||
}, [router.isReady, router.query, key, defaultValue]); | ||
|
||
useEffect(() => { | ||
const handleRouteChange = () => { | ||
const valueFromQuery = router.query[key]; | ||
setState(parseValue(valueFromQuery, defaultValue)); | ||
}; | ||
|
||
router.events.on('routeChangeComplete', handleRouteChange); | ||
return () => { | ||
router.events.off('routeChangeComplete', handleRouteChange); | ||
}; | ||
}, [router.events, key, defaultValue]); | ||
|
||
const setQueryState = (newValue: T) => { | ||
const newQuery = { ...router.query }; | ||
|
||
if (ignoreValues.includes(newValue) || newValue === defaultValue) { | ||
delete newQuery[key]; | ||
} else { | ||
newQuery[key] = stringifyValue(newValue) as string; | ||
} | ||
|
||
router.push({ pathname: router.pathname, query: newQuery }, undefined, { shallow: true }); | ||
setState(newValue); | ||
}; | ||
|
||
return [state, setQueryState]; | ||
} | ||
|
||
function parseValue<T extends QueryValue>(value: string | string[] | undefined, defaultValue: T): T { | ||
if (value === undefined || Array.isArray(value)) return defaultValue; | ||
if (value === 'true') return true as T; | ||
if (value === 'false') return false as T; | ||
if (!isNaN(Number(value))) return Number(value) as T; | ||
return value as unknown as T; | ||
} | ||
|
||
function stringifyValue(value: QueryValue): string { | ||
if (typeof value === 'boolean' || typeof value === 'number') return String(value); | ||
return value as string; | ||
} | ||
|
||
export default useQueryState; |
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