Skip to content

Conversation

@YingHuaLiu
Copy link

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.

@otakustay
Copy link
Member

这个hook的类型设计不严谨,比如这样的:

const [x, y, z] = useSearchParamsArray(['x', 'y']);
const [x, y] = useSearchParamsArray(['x', 'y', 'z']);

也能通过类型验证,这导致可能在代码维护过程中,删了一个param的时候,左右不同步就会遗留问题下来

建议用tuple+重载做一下类型优化,数量超出一定范围(比如5个以上)时才转到弱限制的数组上

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants