react-hooks
react-hooks copied to clipboard
feat(router): add a more practical hook
When I want to get some search params, I find that useSearchParam is too complex.So I write this hook to simplify code. e.g.
// before: use useSearchParam
const Component = () => {
const a = useSearchParam('a');
const b = useSearchParam('b');
const c = useSearchParam('c');
}
// now: use useSearchParamArray
const Component = () => {
const [a, b, c] = useSearchParamArray(['a', 'b', 'c']);
}
Welcome any correction.
这个hook的类型设计不严谨,比如这样的:
const [x, y, z] = useSearchParamsArray(['x', 'y']);
const [x, y] = useSearchParamsArray(['x', 'y', 'z']);
也能通过类型验证,这导致可能在代码维护过程中,删了一个param的时候,左右不同步就会遗留问题下来
建议用tuple+重载做一下类型优化,数量超出一定范围(比如5个以上)时才转到弱限制的数组上